简体   繁体   中英

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. 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.

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:

select *
from TableName
where (Leagues,Matches,Line) in (
    select Leagues,Matches,Line
    from TableName
    where InDict='''No'''
)

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

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