识别表中的重复数据

时间:2016-05-17 07:06:04

标签: sql postgresql

如何找到具有相同身份但不同密钥的动物,即相同的" alt_anim_ident"但不同的" animals_key"

Animals Table

2 个答案:

答案 0 :(得分:2)

SELECT yt1.* , yt2.* /*SELECT both data, you can put * if you want too */
FROM yourtable yt1   
INNER JOIN yourtable yt2 /*Do self-checking*/
ON yt1.alt_anim_ident = yt2.alt_anim_ident /*Check those with the same alt_anim_ident*/
WHERE yt11.animals_key <> yt2.animals_key; /*Eliminates data which pointing to itself*/ 

答案 1 :(得分:2)

SELECT * 
FROM thetable t                                   -- How to find animals
WHERE EXISTS (                                    -- where (different animals) exist
        SELECT * FROM thetable x 
        WHERE x.alt_anim_ident = t.alt_anim_ident -- that have same ident
        AND x.animals_key <> t.animals_key        -- but different key
        )
ORDER BY t.alt_anim_ident, t.animals_key
        ;