我有一個表,如下所示:MySQL的複雜的查詢需要建議
id | sender | receiver | time
1 | [email protected] | [email protected] | 2014-07-04 22:50:16
2 | [email protected] | [email protected] | 2014-07-04 22:51:20
3 | [email protected] | [email protected] | 2014-07-04 22:51:41
4 | [email protected] | [email protected] | 2014-07-04 22:52:45
5 | [email protected] | [email protected] | 2014-07-04 22:52:58
6 | [email protected] | [email protected] | 2014-07-04 22:53:33
7 | [email protected] | [email protected] | 2014-07-04 22:55:53
我想不同的發送器/接收器具有按時間倒序用「[email protected]」爲了一個條目。因此,輸出會像下面
id | sender | receiver | time
6 | [email protected] | [email protected] | 2014-07-04 22:53:33
7 | [email protected] | [email protected] | 2014-07-04 22:55:53
我這個努力,但工作不正常
SELECT *
FROM (
SELECT *
FROM `message`
ORDER BY `time` DESC
) AS `message`
WHERE (
message.sender = '[email protected]'
OR message.receiver = '[email protected]'
)
GROUP BY message.receiver, message.sender
請指引我正確的方式來實現我的目標。
所以你想要最近一行where sender = felix和最近一行接收者是felix?你可以用UNION來做到這一點。 – Strawberry