查找两个实体共享MySQL的列

查找两个实体共享MySQL的列,mysql,Mysql,所以我有一张叫做用户聊天室的桌子: mysql> describe users_chat_rooms; +---------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+------------------+------+-----+---------+

所以我有一张叫做用户聊天室的桌子:

mysql> describe users_chat_rooms;
+---------+------------------+------+-----+---------+----------------+
| Field   | Type             | Null | Key | Default | Extra          |
+---------+------------------+------+-----+---------+----------------+
| id      | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| user_id | int(11)          | NO   | MUL | NULL    |                |
| chat_id | int(11) unsigned | NO   | MUL | NULL    |                |
+---------+------------------+------+-----+---------+----------------+
我有两个拥有唯一用户id的用户,我正在尝试找出他们是否有共同的聊天id


谢谢

这样的查询应该可以做到:

select chat_id
from users_chat_rooms
where user_id in (#USERID1#, #USERID2#)
group by chat_id
having count(distinct user_id) = 2

这样的查询应该可以做到:

select chat_id
from users_chat_rooms
where user_id in (#USERID1#, #USERID2#)
group by chat_id
having count(distinct user_id) = 2
我想这样加入:

select ucr1.chat_id from users_chat_room ucr1 
inner join users_chat_room ucr2 on ucr1.chat_id = ucr2.chat_id
where ucr1.user_id = :userid1 and ucr2.user_id = :userid2
我想这样加入:

select ucr1.chat_id from users_chat_room ucr1 
inner join users_chat_room ucr2 on ucr1.chat_id = ucr2.chat_id
where ucr1.user_id = :userid1 and ucr2.user_id = :userid2

你能告诉我们到目前为止你做了什么吗?你能告诉我们到目前为止你做了什么吗?这很有效,但我发现另一个解决方案更简单。谢谢这很好,但我发现另一个解决方案更简单。谢谢太棒了,谢谢!非常简单易懂!太棒了,谢谢!非常简单易懂!