选择所有用户ID匹配的id

时间:2012-01-24 19:52:05

标签: mysql

好的,我从php脚本中获得了一个包含多个用户id的数组。

现在我有了这个mysql表:

+ - - - - - - - - - - - + - - - - - +
¦ fk_conversation       ¦   fk_user ¦
+ - - - - - - - - - - - + - - - - - +
¦ 1                     ¦     2     ¦
+ - - - - - - - - - - - + - - - - - +
¦ 1                     ¦     3     ¦
+ - - - - - - - - - - - + - - - - - +
¦ 1                     ¦     4     ¦
+ - - - - - - - - - - - + - - - - - +
¦ 2                     ¦     2     ¦
+ - - - - - - - - - - - + - - - - - +
¦ 2                     ¦     4     ¦
+ - - - - - - - - - - - + - - - - - +

如果我得到数组(2,4)我需要得到fk_conversation键= 2但是如果我得到数组(2,3,4)我应该得到fk_conversation键= 1。

我一直希望得到fk_conversation id,其中所有连接的fk_user ID都在数组中。

2 个答案:

答案 0 :(得分:2)

select fk_conversation
from my_table
where fk_user in ( ARRAY VALUES )
and fk_conversation in (
    select fk_conversation
    from my_table
    group by fk_conversation having count(distinct fk_user) = ARRAY SIZE
)
group by fk_conversation
having count(distinct fk_user) = ARRAY SIZE;

答案 1 :(得分:0)

尝试:

select fk_conversation 
    from table 
    where fk_user in ( comma separated array_values ) 
    group by fk_conversation 
    having count(fk_conversation) = array_size