tim*_*enz 2 php mysql group-by
这是我的桌子
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 ..
Run Code Online (Sandbox Code Playgroud)
我想和他的朋友像这样列出乔恩的最新消息
id | sender | receiver | msg
-------------------------------------------
13 | jon | alex | 5 mnt ..
11 | jack | jon | watch the gas
6 | jon | adel | okay
Run Code Online (Sandbox Code Playgroud)
如何查询,以获得该结果?
用这个:
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;
Run Code Online (Sandbox Code Playgroud)
编辑:感谢您对ypercube的编辑,我真的在这里忘记了一些重要的事情:)