SQL - select rows that have the same value in two columns

前端 未结 4 756
被撕碎了的回忆
被撕碎了的回忆 2021-01-04 19:50

The solution to the topic is evading me.

I have a table looking like (beyond other fields that have nothing to do with my question):

NAME,CARDNUMBER,MEMBERTY

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-04 20:32

    Since you mentioned names can be duplicated, and that a duplicate name still means is a different person and should show up in the result set, we need to use a GROUP BY HAVING COUNT(*) > 1 in order to truly detect dupes. Then join this back to the main table to get your full result list.

    Also since from your comments, it sounds like you are wrapping this into a view, you'll need to separate out the subquery.

    CREATE VIEW DUP_CARDS
    AS
    SELECT CARDNUMBER, MEMBERTYPE
    FROM mytable t2
    GROUP BY CARDNUMBER, MEMBERTYPE
    HAVING COUNT(*) > 1
    
    CREATE VIEW DUP_ROWS
    AS
    SELECT t1.*
    FROM mytable AS t1
    INNER JOIN DUP_CARDS AS DUP
    ON (T1.CARDNUMBER = DUP.CARDNUMBER AND T1.MEMBERTYPE = DUP.MEMBERTYPE )
    

    SQL Fiddle Example

提交回复
热议问题