【问题标题】:How to make a query to GROUP BY x DESC如何查询 GROUP BY x DESC
【发布时间】:2012-02-16 10:07:46
【问题描述】:

下面的 SELECT 语句

select * 
from messages 
where receiverID = '5' 
group BY senderID 
order by id DESC

数据库:

id | senderID | receiverID | message
1  |  245     |    5       | test 1
2  |  89      |    5       | test 2
3  |  79      |    5       | test 3
4  |  245     |    5       | test 4
5  |  245     |    5       | test 5

对于 senderID=245,我希望返回 id=5 的行,但它返回的行 id=1,但我想要最后一行。如何实现?

返回:

id | senderID | receiverID | message
1  |  245     |    5       | test 1
2  |  89      |    5       | test 2
3  |  79      |    5       | test 3

哦,我成功了:D

所以这是有效的代码,对于任何有类似问题的人

     SELECT * FROM ( SELECT * FROM messages WHERE
 receiverID = '5' ORDER BY id DESC) AS m GROUP BY senderID ORDER BY id DESC

【问题讨论】:

  • 您能改写一下这个问题吗?也许是您尝试实现的示例列表结果
  • 你为什么在这里使用GROUP BY
  • 我使用 GROUP BY 因为我想要每个 senderID 的最后一行。

标签: mysql sql group-by sql-order-by


【解决方案1】:

这是不可能的。您必须执行以下操作:

[...] WHERE `id` = (SELECT MAX(`id`) FROM `messages` WHERE `receiverID` = '5')

【讨论】:

    【解决方案2】:

    我个人会考虑一个子查询,类似这样的东西应该可以为你完成这项工作

    SELECT messagesOrdered.*
    FROM (
      SELECT * 
      FROM messages 
      WHERE receiverID = '5' 
      ORDER BY id DESC
    ) AS messagesOrdered
    GROUP BY senderID
    

    您可能希望根据表的大小检查您设置了哪些键。

    使用 MAX 的问题在于,如果您在 id 字段上使用 MAX,那么它将获得您要查找的数字,但是在另一个字段上使用 MAX 不会获得与该 id 匹配的数据。使用 subquery 方法,内部查询进行排序,然后外部的 GROUP 将根据内部查询中的行顺序进行分组。

    【讨论】:

    • 这几乎奏效了.. 是的,它获取了 senderID 的最后一行,但是 senderID 的行只有 1 行,在组出现在分组行之前
    • 啊,我正要评论说您仍然可以订购外部查询,但并不是 100% 清楚您究竟想要什么。很高兴看到它有所帮助。
    【解决方案3】:
    SELECT * FROM messages m
    JOIN 
    ( SELECT senderID, MAX(id) AS last
      FROM messages 
      WHERE receiverID = '5' 
      GROUP BY senderID ) mg
    ON m.id = mg.last
    

    【讨论】:

      【解决方案4】:

      不确定我是否完全理解你的问题,但听起来你想要的:

      select max(id), 
             senderId, 
             max(receiverId), 
             max(message)
      from messages 
      where receiverID = '5' 
      group BY senderID 
      order by id DESC
      

      请注意,您还需要将消息包含到您的聚合中,否则您将得到不可预测的结果(其他 DBMS 不允许省略 max(message),但 MySQL 只会从组中返回随机行)。

      【讨论】:

        【解决方案5】:

        这是我的:)

        select m1.* from messages m1
        left join messages m2
        on m1.senderid = m2.senderid and m1.id < m2.id
        where m2.id is null and receiverID = '5' 
        

        根据您的示例,这将返回:

        +----+----------+------------+---------+
        | ID | SENDERID | RECEIVERID | MESSAGE |
        +----+----------+------------+---------+
        |  2 |       89 |          5 | test 2  |
        |  3 |       79 |          5 | test 3  |
        |  5 |      245 |          5 | test 5  |
        +----+----------+------------+---------+
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-03-09
          • 2013-09-20
          • 2011-11-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-07
          • 2013-05-30
          相关资源
          最近更新 更多