2014-11-02 62 views
0

我有以下數據:
我= my_userid;

表:消息

MESSAGE_ID | message_from | message_to
1 |我| |用戶
2 |用戶|我
sqlite的DISTINCT查詢從 - 要,要 - 從

運行的查詢提供了兩行(SELECT DISTINCT message_from,message_to FROM消息,其中message_from = ' 「+我+」' OR message_to = ' 「+我+」'「);

使用,或者因爲我的ID可以從在(發送時)或TO(當其他用戶發送消息我)

- 然而,這返回兩行,我希望它返回一個行,因爲如果切換到 - 從你得到相同的ID,那麼怎麼能這可以在查詢中完成。謝謝

回答

1

您可以使用min()max()作爲標量個功能:

select distinct min(message_from, message_to) as m1, max(message_from, message_to) as m2 
from message 
where message_from ='"+me+"' OR message_to ='"+me+"'" 

這些等同於在其他數據庫least()greatest()

+0

這實際工作,非常感謝你。 – Fihox 2014-11-02 15:20:22