创建 GROUP BY 查询以显示最新行

Create a GROUP BY query to show the latest row

所以我的 table 是:
user_msgs: http://sqlfiddle.com/#!9/7d6a9
token_msgs: http://sqlfiddle.com/#!9/3ac0f

仅列出这 4 位用户。当一个用户向另一个用户发送消息时,查询通过检查 token_msgs table 的 from_id 和 to_id 来检查这两个用户之间是否已经开始通信如果不存在标记,则创建 token 并在 user_msgs table 中使用它。所以token是这2个table中的唯一字段。

现在,我想列出 user1 与之开始对话的用户。因此,如果 from_idto_id 包含 1,则应列出这些对话。

user_msgs table 中有多个相同用户的对话行。

我想我需要使用 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');

让我们试试这个(在 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 这是因为 sum 使得两人之间的每次对话都是独一无二的:比如从 1 到 3 与从 3 到 1 相同。不需要额外的 table,并且更难维护

更新:

因为有时 GROUPing 在 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