代码之家  ›  专栏  ›  技术社区  ›  John Smith

查询房间sqlite检查两列

  •  -1
  • John Smith  · 技术社区  · 7 年前

    我有一张这样的对话桌

    会话

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

    因此,基本上,发送者id可以位于recipient\u id列中,这取决于首先发起对话的任何人。

    如何进行查询,例如传递recipient\u id,2。并检查它是否在sender\u id列或recipient\u id列中。类似于:

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

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   Neodreadlord    7 年前

    您可以尝试以下操作:

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

    这和您要求的一样,但我在使用workbench时已经这样安排了。我在声明中使用了OR。

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