Filter Results in SQL

后端 未结 4 1452
臣服心动
臣服心动 2020-12-04 04:31

Suppose I have a table

  id            value
------        ---------
  10              123
  10              422
  11              441
  11              986
         


        
相关标签:
4条回答
  • 2020-12-04 04:40
    select a2.*
    from MyTable a2
    inner join
    (
    select a1.id
    from MyTable a1
    group by a1.id
    having count(*) > 1
    ) a3
    on a3.id = a2.id
    
    0 讨论(0)
  • 2020-12-04 04:48

    You can use this query :

    SELECT * from table where id in 
    ( SELECT id FROM table group by id having count(id) > 1 )
    
    0 讨论(0)
  • 2020-12-04 04:57

    Assuming a UNIQUE KEY can be formed on (id,value)...

    SELECT DISTINCT x.*
               FROM my_table x
               JOIN my_table y
                 ON y.id = x.id
                AND y.value <> x.value
    

    If a UNIQUE KEY cannot be formed on (id,value), then this isn't really a table in a strict RDBMS sense.

    0 讨论(0)
  • 2020-12-04 05:02

    With mysql 8+ or mariadb 10.2+, you would use the count window function:

    select id, value
    from (
        select id, value, count(id) over (partition by id) as num_values
        from sometable
    ) foo
    where num_values > 1;
    
    0 讨论(0)
提交回复
热议问题