所以我的表是:
user_msgs
:http://sqlfiddle.com/#!9/7d6a9
token_msgs
:http://sqlfiddle.com/#!9/3ac0f創建GROUP BY查詢,以顯示最新的排
只有這些用戶4所列。當用戶將消息發送給其他用戶,查詢檢查是否存在已通過檢查token_msgs
表的from_id和to_id開始的2個用戶之間的通信,如果沒有令牌存在,創建token
和使用,在user_msgs
表。所以令牌是這兩個表中唯一的字段。
現在,我想列出與之user1
已經開始對話的用戶。因此,如果from_id
或to_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
請構建查詢幫助。
謝謝。
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');
認沽DDL/DML。不要鏈接到外部網站 – hjpotter92
請與預期的結果作爲文本更新的問題,並且標記相關的RDBMS(不SQLServer的都和MySQL的),如果你希望你的查詢,以便對兩種版本,請指定 – TheGameiswar
@TheGameiswar做 – Somename