代码之家  ›  专栏  ›  技术社区  ›  user3206440

查找具有多个不同列值的记录

  •  0
  • user3206440  · 技术社区  · 6 年前

    给定表 t1 带柱 Id (文本、主键)和 place (文本)如下。

    +-------+-----------+
    |  Id   |   place   |
    +-------+-----------+
    | abcde |  Santori  |
    | bcdef |  Krypt    |
    | cdefg |  Bali     |
    | defgh |  Bangkok  |
    | abcde |  Colombo  |
    +-------+-----------+
    

    我需要找出 身份证件 有多个不同的 地方 . 在上述示例中,输出应为

    +-------+-----------+
    |  Id   |   place   |
    +-------+-----------+
    | abcde |  Santori  |
    | abcde |  Colombo  |
    +-------+-----------+
    
    3 回复  |  直到 6 年前
        1
  •  1
  •   Vinh Can Code    6 年前

    SELECT ID, PLACE FROM T1 as A 
    WHERE A.ID IN 
    (SELECT ID FROM T1 AS B 
    GROUP BY ID 
    HAVING count(*) > 1
    )
    
        2
  •  2
  •   Yogesh Sharma    6 年前

    exists

    select t.*
    from table t
    where exists (select 1 from table t1 where t1.id = t.id and t1.place <> t.place);
    
        3
  •  1
  •   Utsav    6 年前

    count distinct place

    Fiddle Example

    select * From T1
    where T1.ID in
    (select ID from T1
     group by ID
    having count(distinct PLACE) > 1
    )