简体   繁体   English

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

[英]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.

相关问题 Select 所有行在一列中包含相同的值 - Select all rows contains same value in a column 仅当 Select 记录包含所有行的相同列值时 - Select record only if it contains same column value for all rows MySQL:全选,除了当一列包含一个特定值而另一列包含另一特定值时 - MySQL: SELECT all except when one column contains one particular value and another contains another specific value 计算来自不同表的特定值的行,两个表都包含同一列? - count rows for particular value from different table both table contains one same column? SELECT *以及与同一表的另一列的ID相关联的所有行 - SELECT * plus all rows that are associated with the IDs of another column of the same table 如何从数据库MySQL的特定列中选择具有相同和最新值的所有行 - How to select all the rows having same and latest value in a particular column from database mysql 选择所有列值相同的行 - Select all rows where the value of column is the same 如何为列的相同值选择所有行? - How to select all rows for same value of column? 如何创建一个表,其中一列包含具有不同ID的所有行的相同值?(有关说明,请参见布局) - How to create a table where one column contains a same value for all rows with different id?(See Layout for clarity) 在包含多个ID(用逗号分隔)的列中选择以ID搜索的行 - Select rows searching with an id in a column that contains multiple ids separated by a comma
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM