MySql多对多只选择连接行中的固定行

时间:2014-03-08 13:20:25

标签: mysql many-to-many

我这里有一个数据库

第一张表是会话表

___________________________
| id | conversation Name  |
__________________________
|  1 | member and officer |
___________________________
|  2 | member and guests  |
___________________________

第二个表是用户表

_________________
| id | name     |
_________________
|  1 |member    |
_________________
|  2 |officer   |
_________________
|  3 |guest     |
_________________

第3个表是conversation_user_involved表

_________________________________
| id_conversation | id_user     |
_________________________________
|              1  | 1           |
_________________________________
|              1  | 2           |
_________________________________
|              2  | 1           |
_________________________________
|              2  | 3           |
_________________________________

问题是如何根据用户选择对话。

假设输入用户参数为1和2,因此意味着用户1是成员,2是官员。 如何选择会话成员和官员?

我尝试了“IN”查询,但它没有用。

1 个答案:

答案 0 :(得分:0)

未经测试的:

select     id_conversation, conversation_Name
from       conversation_user_involved
inner join conversation on (id_conversation = conversation.id)
where      id_user in (1, 2)
group by   id_conversation
having     count(id_conversation) = 2