查找第二列entires共享MySQL

时间:2016-01-13 19:54:47

标签: mysql

所以我有一个名为users_chat_rooms的表:

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

我有两个拥有唯一user_id的用户,我正在试图弄清楚如何找出他们共同的chat_id是什么。

谢谢!

2 个答案:

答案 0 :(得分:2)

我这样加入:

print("\n".join(zip(*addresses)[0]))
#or maybe just 
print(zip(*addresses)[0]) 

答案 1 :(得分:1)

这样的查询应该可以解决问题:

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