簡體   English   中英

如果某一列包含特定值,則選擇具有相同ID的所有行

[英]Select all rows with same IDs if one column contains a particular value

我有一張表,大致如下:

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列中包含“否”值。 因此,在上面的示例中,查詢將返回:

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的新手,因此正在努力為此目的找到正確的查詢。

任何幫助是極大的贊賞!

這應該工作:

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允許您在in運算符中使用多個列子查詢:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM