查询房间sqlite检查两列

问题描述 投票:-1回答:1

我有一个这样的会话表

会话

+----+-----------+--------------+
| Id | sender_id | recipient_id |
+----+-----------+--------------+
|  0 |         3 |            2 |
|  1 |         2 |            1 |
|  2 |         1 |            3 |
+----+-----------+--------------+

所以基本上,sender_id可以在recipient_id列中,具体取决于首先发起会话的任何人。

如何进行查询,例如传递recipient_id,2。并检查它是否在sender_id列或recipient_id列中。就像是:

@Query("select * from conversation where sender_id = :userId OR recipient_id = :userId  ")
LiveData<Conversation> getAConversationByUserId(long userId);

这会有用吗?没试过。我应该使用|| or OR吗?

android sql sqlite android-room android-architecture-components
1个回答
1
投票

你可以尝试以下方法:

    SELECT * 
    FROM conversation
    WHERE sender_id = user_id OR recipient_id = user_id;

它和你问的一样,但是当我使用工作台时,我已经这样做了。我在声明中使用了OR。

正如评论中指出的那样,在SQLite中||特别是连接运算符,与OR不同。为我以前的错误道歉!

© www.soinside.com 2019 - 2024. All rights reserved.