查询房间sqlite检查两列

Query room sqlite checking two columns

我有一个对话table像这样

对话

+----+-----------+--------------+
| 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 吗?

您可以尝试以下方法:

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

这和你问的一样,但我在使用 workbench 时是这样安排的。我在语句中使用了 OR。

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