2016-08-15 56 views
0

所以我的表是:
user_msgshttp://sqlfiddle.com/#!9/7d6a9
token_msgshttp://sqlfiddle.com/#!9/3ac0f創建GROUP BY查詢,以顯示最新的排

只有這些用戶4所列。當用戶將消息發送給其他用戶,查詢檢查是否存在已通過檢查token_msgs表的from_id和to_id開始的2個用戶之間的通信,如果沒有令牌存在,創建token和使用,在user_msgs表。所以令牌是這兩個表中唯一的字段。

現在,我想列出與之user1已經開始對話的用戶。因此,如果from_idto_id包括1這些對話應該列出。

有在user_msgs表相同的用戶會話多行。

我想我需要使用group_concat,但不能確定。我試圖建立查詢,以做同樣展示了最新的談話頂部,因此ORDER BY time DESC

SELECT * FROM (SELECT * FROM user_msgs ORDER BY time DESC) as temp_messages GROUP BY token 

請構建查詢幫助。

enter image description here

謝謝。

CREATE TABLE `token_msgs` (
    `id` int(11) NOT NULL, 
    `from_id` int(100) NOT NULL, 
    `to_id` int(100) NOT NULL, 
    `token` varchar(50) NOT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

-- 
-- Dumping data for table `token_msgs` 
-- 

INSERT INTO `token_msgs` (`id`, `from_id`, `to_id`, `token`) VALUES 
(1, 1, 2, '1omcda84om2'), 
(2, 1, 3, '1omd0666om3'), 
(3, 4, 1, '4om6713bom1'), 
(4, 3, 4, '3om0e1abom4'); 


--- 


CREATE TABLE `user_msgs` (
    `id` int(11) NOT NULL, 
    `token` varchar(50) NOT NULL, 
    `from_id` int(50) NOT NULL, 
    `to_id` int(50) NOT NULL, 
    `message` text NOT NULL, 
    `time` datetime NOT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

-- 
-- Dumping data for table `user_msgs` 
-- 

INSERT INTO `user_msgs` (`id`, `token`, `from_id`, `to_id`, `message`, `time`) VALUES 
(1, '1omcda84om2', 1, 2, '1 => 2\r\nCan I have your picture so I can show Santa what I want for Christmas?', '2016-08-14 22:50:34'), 
(2, '1omcda84om2', 2, 1, 'Makeup tip: You\'re not in the circus.\r\n2=>1', '2016-08-14 22:51:26'), 
(3, '1omd0666om3', 1, 3, 'Behind every fat woman there is a beautiful woman. No seriously, your in the way. 1=>3', '2016-08-14 22:52:08'), 
(4, '1omd0666om3', 3, 1, 'Me: Siri, why am I alone? Siri: *opens front facing camera*', '2016-08-14 22:53:24'), 
(5, '1omcda84om2', 1, 2, 'I know milk does a body good, but damn girl, how much have you been drinking? 1 => 2', '2016-08-14 22:54:36'), 
(6, '4om6713bom1', 4, 1, 'Hi, Im interested in your profile. Please send your contact number and I will call you.', '2016-08-15 00:18:11'), 
(7, '3om0e1abom4', 3, 4, 'Girl you\'re like a car accident, cause I just can\'t look away. 3=>4', '2016-08-15 00:42:57'), 
(8, '3om0e1abom4', 3, 4, 'Hola!! \r\n3=>4', '2016-08-15 00:43:34'), 
(9, '1omd0666om3', 3, 1, 'Sometext from 3=>1', '2016-08-15 13:53:54'), 
(10, '3om0e1abom4', 3, 4, 'More from 3->4', '2016-08-15 13:54:46'); 
+0

認沽DDL/DML。不要鏈接到外部網站 – hjpotter92

+1

請與預期的結果作爲文本更新的問題,並且標記相關的RDBMS(不SQLServer的都和MySQL的),如果你希望你的查詢,以便對兩種版本,請指定 – TheGameiswar

+0

@TheGameiswar做 – Somename

回答

1

讓我們試試這個(上fiddle):

SELECT * 
FROM (SELECT * FROM user_msgs 
    WHERE from_id = 1 OR to_id = 1 
    ORDER BY id DESC 
) main 
GROUP BY from_id + to_id 
ORDER BY id DESC 

事情提GROUP BY from_id + to_id這是因爲總和使得它獨特的兩個人之間的每個對話:就像從1至3是一樣的,從3 1。無需額外的表,這使得它很難維持

UPDATE:

因爲有時GROUP ING在MySQL怪異的作品我創建的新方法,這一問題:在問題本身

SELECT 
    a.* 
FROM user_msgs a 
LEFT JOIN user_msgs b 
    ON ((b.`from_id` = a.`from_id` AND b.`to_id` = a.`to_id`) 
     OR (b.`from_id` = a.`to_id` AND b.`to_id` = a.`from_id`)) 
    AND a.`id` < b.`id` 
WHERE (a.from_id = 1 OR a.to_id = 1) 
    AND b.`id` IS NULL 
ORDER BY a.id DESC 
+0

它在小提琴中工作,但不在本地。進入phpMyAdmin的控制檯,並與PHP太..可能是什麼問題?請幫忙。在小提琴中工作的人正是我想要的。 http://imgur.com/a/7gWQQ我做錯了什麼?請幫忙。 – Somename

+0

看着我的結果清楚地看到,它撿起了2個用戶之間的第一次對話,並且用你的查詢拾起了最後一個,我想要什麼。 – Somename

+0

是你的數據大嗎?你有索引嗎?什麼是錯誤? –