How to return rows that have the same column values in MySql

前端 未结 3 1592
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 22:47

Lets consider the following table-

ID Score
1  95

2  100

3  88

4  100

5  73

I am a total SQL noob but how do I return the Scores featur

3条回答
  •  不知归路
    2020-11-21 23:14

    select Score
    from tbl a
    where a.ID = 2 -- based off Score with ID = 2
        --include Score only if it exists with ID 6 also
        and exists (
            select 1
            from tbl b
            where b.Score = a.Score and b.ID = 6
        )
        -- optional?  ignore Score that exists with other ids as well
        and not exists (
            select 1
            from tbl c
            where c.Score = a.Score and c.ID not in (2, 6)
        )
    

提交回复
热议问题