简体   繁体   English

SQL,如何通过已知值获取在另一列中具有相同值的行?

[英]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. 我有两个user_id值,并且我希望返回其中user_id是两个值之一和thread_id相同的行。

Let's say, Johnny(1) has a thread with Mark(2). 假设Johnny(1)与Mark(2)有一个线程。 Now I know Johny and Mark but I want to get the thread_id of the thread they have in common. 现在,我认识了Johny和Mark,但我想获取它们共同的线程的thread_id。

I'd appreciate a SQL hint, but if you know Laravel and Eloquent, that would be even better. 我会很高兴看到SQL提示,但是如果您了解Laravel和Eloquent,那会更好。

In SQL: 在SQL中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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