gpt4 book ai didi

MySQL Query group by 获取最后一个id

转载 作者:行者123 更新时间:2023-11-29 01:10:13 25 4
gpt4 key购买 nike

这是我的 table

id | sender | receiver | msg  
-----------------------------
1 | jon | jack | buzz ...
2 | jack | jon | ?
3 | adel | jon | met me soon
4 | jon | adel | okay
5 | alex | jon | where ar u ?
6 | jon | adel | okay
7 | adel | alex | don't forget the party
8 | jon | jack | may i borrow ur car
9 | alex | adel | of course
10 | jack | jon | ok
11 | jack | jon | watch the gas
12 | alex | jon | i'm dying here
13 | jon | alex | 5 mnt ..

我想像这样获取 jon 和他 friend 的最后消息列表

id | sender | receiver | msg
-------------------------------------------
13 | jon | alex | 5 mnt ..
11 | jack | jon | watch the gas
6 | jon | adel | okay

如何查询,得到那个结果?

最佳答案

使用这个:

SELECT *
FROM table
WHERE id IN (
SELECT MAX(id)
FROM table
WHERE sender = 'jon'
OR receiver = 'jon'
GROUP BY IF(sender = "jon", receiver, sender)
)
ORDER BY id DESC;

编辑:感谢ypercube的编辑,我真的忘记了一些重要的东西:)

关于MySQL Query group by 获取最后一个id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11428037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com