[英]Select all rows with same IDs if one column contains a particular value
I have a table that looks roughly as follows: 我有一张表,大致如下:
Runners Leagues Matches Line InDict
Team 1 LeagueA Match 1 Line 1 'Yes'
Team 2 LeagueA Match 1 Line 1 'Yes'
Team 3 LeagueA Match 1 Line 1 'No'
Team 1 LeagueA Match 4 Line 1 'Yes'
Team 2 LeagueA Match 4 Line 1 'Yes'
Team 1 LeagueA Match 1 Line 2 'Yes'
Team 2 LeagueA Match 1 Line 2 'Yes'
Team 3 LeagueA Match 1 Line 2 'Yes'
Team 1 LeagueB Match 5 Line 8 'No'
Team 2 LeagueB Match 5 Line 8 'Yes'
What I would like to do is select all rows where the Leagues/Matches/Line columns are the same if one of those rows contains the value 'No' in the InDict column. 我想做的是选择“联赛/比赛/线”列相同的所有行,如果其中之一在InDict列中包含“否”值。 So in the above example, the query would return:
因此,在上面的示例中,查询将返回:
Team 1 LeagueA Match 1 Line 1 'Yes'
Team 2 LeagueA Match 1 Line 1 'Yes'
Team 3 LeagueA Match 1 Line 1 'No'
Team 1 LeagueB Match 5 Line 8 'No'
Team 2 LeagueB Match 5 Line 8 'Yes'
I'm new to mysql, so I'm struggling to find the correct query for this purpose. 我是mysql的新手,因此正在努力为此目的找到正确的查询。
Any help is greatly appreciated! 任何帮助是极大的赞赏!
This should work: 这应该工作:
select * from tableName a where
exists(select * from tableName b where
b.league=a.league and
b.matches=a.matches and
b.line=a.line and
b.inDict="No")
MySQL allows you to use multiple columns subqueries in in
operator: MySQL允许您在
in
运算符中使用多个列子查询:
select *
from TableName
where (Leagues,Matches,Line) in (
select Leagues,Matches,Line
from TableName
where InDict='''No'''
)
http://sqlfiddle.com/#!2/ddf5c/1 http://sqlfiddle.com/#!2/ddf5c/1
select * from yourtable
where concat(leagues,Matche,line) in(
select concat(leagues,Matche,line) as v
from yourtable
where InDict = 'No'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.