简体   繁体   English

查询返回其他行

[英]Query returns additional rows

I have this kind of table for simple chat: 我有这种用于简单聊天的表格:

messages table structure
+----+---------+-----------+---------+------+------+
| id | to_user | from_user | message | read | sent |
+----+---------+-----------+---------+------+------+

And i need to get list of each conversation which looks like that 我需要获取每个看起来像这样的对话的列表

Username ----  Message ---- Date

I am using this query to do it: 我正在使用此查询来做到这一点:

SELECT *
FROM `messages`
WHERE `sent`
IN (
   SELECT MAX( `sent` )
   FROM `messages`
   WHERE `from_user` = '1' --id of user who is requesting the list
   OR `to_user` = '1'  --id of user who is requesting the list
   GROUP BY `to_user` , `from_user`
   )
LIMIT 0 , 30

And this works almost fine, my problem is that it returns me not the last message of that conversation but last message from each user so let's say user 1 and 2 is talking and i'm getting this list, this is what i get: 这几乎可以正常工作,我的问题是它返回的不是我的对话的最后一条消息,而是每个用户的最后一条消息,因此,假设用户12正在交谈,并且我正在获取此列表,这就是我得到的:

+----+---------+-----------+-----------------------+------+---------------------+
| id | to_user | from_user | message               | read | sent                |
+----+---------+-----------+-----------------------+------+---------------------+
|  3 |       2 |         1 | Message 1 from user 1 |    0 | 2012-01-11 13:20:54 |
|  4 |       1 |         2 | Message 1 from user 2 |    0 | 2012-01-11 13:24:59 |
+----+---------+-----------+-----------------------+------+---------------------+

And i would like to get only last message which is 4 , cause sent field is the highest in 4th record so how can i solve it? 而且我只想获取最后一条消息,即4 ,因为sent字段是第4条记录中最高的字段,所以我该如何解决呢?

EDIT After deleting group by i'm getting only one message even if user was talking with more than one user 编辑删除group by即使用户与多个用户交谈,我也只收到一条消息

SELECT *
FROM `messages`
WHERE `sent`
IN (
   SELECT MAX( `sent` )
   FROM `messages`
   WHERE (`from_user` = '1' OR `to_user` = '1')
   )
LIMIT 0 , 30

the groupby is going to conbine them i believe. 我相信,groupby将束缚他们。

You are getting the last message from each user because you have done GROUP BY for both: to_user and from_user. 您正在从每个用户那里获得最后一条消息,因为您已经为to_user和from_user都完成了GROUP BY。

There is no need to use GROUP BY clause in your query. 您的查询中无需使用GROUP BY子句。

Here's how you do it: 这是您的操作方式:

SELECT *
FROM (SELECT * 
  FROM messages
  WHERE from_user = ?
  OR to_user = ?
  ORDER by from_user, to_user, sent DESC
) x
GROUP BY from_user, to_user
ORDER BY sent DESC
LIMIT 1;

In mysql, a group by without aggregating the other columns returns the first row for each group. 在mysql中,通过聚合其他列的组返回每个组的第一行。 By selecting form an ordered row set (the inner query) we get the most recent row for each conversation. 通过选择有序行集(内部查询)的形式,我们可以获取每次会话的最新行。

Remove the group by clause in your in statement--it's useless in this case. 删除in语句中的group by子句-在这种情况下,它是无用的。 It's returning a sent timestamp for each distinct pairing of to_user and from_user . 它为to_userfrom_user每个不同配对返回sent时间戳。 You really just want the max sent where to_user or from_user equal some value. 您实际上只希望在to_userfrom_user等于某个值的情况下sent的最大值。 Lose the group by , and you'll return exactly one record showing the latest message either to or from a user. 通过丢失group by ,您将只返回一条记录,该记录显示了发给用户或来自用户的最新消息。

It looks like this: 看起来像这样:

SELECT *
FROM `messages`
WHERE `sent`
IN (
   SELECT MAX( `sent` )
   FROM `messages`
   WHERE `from_user` = '1' --id of user who is requesting the list
   OR `to_user` = '1'  --id of user who is requesting the list
   )
LIMIT 0 , 30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM