简体   繁体   中英

SQL, how to get rows by known value that have the same value in another column?

I need your help.

I have a table like this:

数据库截图

So I want to search for a specific thread. I have two user_id values and I want the rows returned where the user_id is one of the two and the thread_id is the same.

Let's say, Johnny(1) has a thread with Mark(2). Now I know Johny and Mark but I want to get the thread_id of the thread they have in common.

I'd appreciate a SQL hint, but if you know Laravel and Eloquent, that would be even better.

In SQL:

SELECT thread_id
FROM mytable
WHERE user_id IN (1, 2)
GROUP BY thread_id
HAVING COUNT(DISTINCT user_id) = 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM