[英]Mysql: how to select rows where the combination of 2 of the column values are unique given a certain other column value
[英]MySQL select where value has multiple values in other rows
首先,很抱歉,如果您之前曾提出过此问题,我确实尝试过搜索,但是找不到我想要的东西。
好的,这是我的桌子。 我想查询一个查询,在该查询中,我将选择具有相同确切值且超过2次的用户ID。
| USER ID|COLOR|
---------|-------
| 1 | BLUE
| 2 | BLUE
| 3 | RED
| 4 | BLUE
所以我的查询在这种情况下会返回
| USER ID|
|--------|
| 1 |
| 2 |
| 4 |
非常感谢你!
您似乎想要选择重复color
两次以上的行。 这是一种方法:
select t.*
from t join
(select color, count(*) as cnt
from t
group by color
) tt
on tt.color = t.color and cnt > 2;
您要求进行2场以上的比赛。 对于2个或更多匹配项,您将使用sgeddes的解决方案:
select t.*
from t
where exists (select 1
from t tt
where t.color = tt.color and t.userid <> tt.userid
);
或者,如果您只想要多个ID:
select color, group_concat(userid)
from t
group by color
having count(*) >= 2;
这是exists
的一种选择:
select userid
from yourtable t
where exists (
select 1
from yourtable t2
where t.userid <> t2.userid and t.color = t2.color
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.