2012-10-18 25 views
1

這應該相當簡單,但我堅持使用它。選擇用戶或用戶發送的郵件

當我做查詢

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消息)。

沒有人知道最好的方法嗎?

謝謝!

+0

請提供給定的輸入 –

+0

u能提供你想要的,什麼結果,現在你所得到的樣品輸出? –

+0

我想你想查看部分不查詢你會獲取所有的用戶4的對話,但點擊頂部的消息剩餘的消息應該來一些jquery的東西是正確的 –

回答

4

SQL Fiddle

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 
+0

我認爲他想查看部分不查詢他將獲取用戶4的所有對話,但在頂部消息的點擊餘下的消息應該來一些jQuery的東西 –

+0

Clodoaldo,我試過這個,但它不工作 - - 你的SQL語法有錯誤;請檢查與您的MySQL服務器版本相對應的手冊,以便在第6行附近'where toid ='4'或fromid ='4')s內部聯接收件箱中使用正確的語法' – Outlaw011

+0

@ Outlaw011您是什麼意思_it不工作_?顯示錯誤?結果不是預期的?解釋一下。 –