SQL - 检查ID是否包含相同的ID值

时间:2016-05-19 13:41:06

标签: mysql sql

我想知道如何在不传递( thread_id )的情况下检查两个用户( user_id )是否在同一个帖子中( thread_id )使用SQL,通过下表的结构。

enter image description here

3 个答案:

答案 0 :(得分:1)

要获得这两个用户所在的所有线程,您可以

select thread_id
from your_table
where user_id in (1,2)
group by thread_id
having count(distinct user_id) = 2

获取具有多个用户的所有线程

select thread_id
from your_table    
group by thread_id
having count(distinct user_id) >= 2

答案 1 :(得分:0)

我不太清楚你想要什么,但据我所知,我认为这是。

select thread_id, COUNT(thread_id) AS dup_count  from participant_thread as participant
GROUP BY thread_id
HAVING (COUNT(thread_id) > 1) 

result scrypt

答案 2 :(得分:-1)

select thread_id, user_id
from thread.thread_table
where thread_id in (
    select thread_id
    from thread.thread_table
    group by thread_id
    having count(user_id) > 1
);