2014-11-02 66 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