簡體   English   中英

選擇用戶或用戶發送的消息

[英]select messages sent by user or to user

這應該相當簡單,但我堅持下去。

當我做查詢

SELECT * FROM `inbox` where toid=4 or fromid=4 order by time desc

,我得到:

id  toid    fromid  message           time
23  48101   4   hello call me     12/23/2011 12:27
6   34584   4   hi there      12/22/2011 15:42
5   34584   4   how are you   12/22/2011 14:08
4   34584   4   say hello     12/22/2011 14:07
3   34584   4   whats up      12/22/2011 14:07
2   4   34584   nice picture      11/24/2010 0:00
1   4   2   this is very interesting!   12/23/2008 0:00

現在,我需要將用戶4和其他用戶之間的對話分組到最后一條消息的一行(如facebook消息一樣)。

有誰知道最好的方法嗎?

謝謝!

SQL小提琴

select i.id, toid, fromid, message, `time`
from
    inbox i
    inner join (
        select max(id) as id
        from inbox
        where toid = 4 or fromid = 4
        group by greatest(toid, fromid), least(toid, fromid)
    ) s on i.id = s.id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM