我有一张用户间的桌子

roomID | userID

我现在想检查一下,两个用户是否在同一个房间
SELECT roomID
FROM   room_to_user
WHERE  userID = 1 AND 2

如何通过加入实现这一点?

最佳答案

进行自连接:

SELECT DISTINCT u1.roomID AS roomID
FROM   user_to_room u1, user_to_room u2
WHERE  u1.userID = 1 AND u2.userID = 2
AND    u1.roomID = u2.roomID;

demo

关于mysql - 使用MySQL检查2个用户是否在同一房间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16511417/

10-09 08:34