如果一列包含特定值,请选择具有相同ID的所有行

时间:2015-02-07 00:35:06

标签: mysql

我的表格大致如下:

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'

如果其中一行在InDict列中包含值“No”,我想要选择Leagues / Matches / Line列相同的所有行。所以在上面的例子中,查询将返回:

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'

我是mysql的新手,所以我很难为此找到正确的查询。

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:1)

这应该有效:

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")

答案 1 :(得分:1)

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

答案 2 :(得分:-1)

select * from yourtable
where concat(leagues,Matche,line) in(
    select concat(leagues,Matche,line) as v
    from yourtable
    where InDict = 'No'
)