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

时间:2016-08-15 13:25:23

标签: mysqli group-concat

所以我的表格是:
user_msgshttp://sqlfiddle.com/#!9/7d6a9
token_msgshttp://sqlfiddle.com/#!9/3ac0f

列出的这四个用户中只有这四个。当用户向另一个用户发送消息时,查询通过检查token_msgs表的from_id和to_id来检查已经启动的那两个用户之间是否存在通信,如果不存在令牌,则创建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');

1 个答案:

答案 0 :(得分: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相同。不需要额外的表,这使得它变得更难保持

<强>更新

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