2015-11-26 29 views
0

我的表是這樣的:僅顯示從表中Postgres的重複行

-------------------------------------- 
| id | name | phone_number | address | 
-------------------------------------- 
| 1 | Ram | 9090909090 | Delhi | 
| 2 | Shyam| 9865444456 | Mumbai | 
| 3 | Mohan| 9756543455 | Chennai | 
| 4 | Ram | 9090909090 | Delhi | 
-------------------------------------- 

我想回到有相同的列數據的行。其結果將是這樣的:

-------------------------------------- 
| id | name | phone_number | address | 
-------------------------------------- 
| 1 | Ram | 9090909090 | Delhi | 
| 4 | Ram | 9090909090 | Delhi | 
-------------------------------------- 

回答

1
SELECT t2.id,t2.name,t2.phone_number,t2.address 
FROM 
(
SELECT name,phone_number,address 
FROM tableName 
GROUP BY name,phone_number,address 
HAVING COUNT(*) > 1 
) AS t1 
INNER JOIN tableName t2 
ON t1.name=t2.name AND t1.phone_number=t2.phone_number AND t1.address=t2.address 
+0

非常感謝,它工作得很好。 –

0
SELECT T1.* 
FROM 
table_name T1 
INNER JOIN table_name T2 ON 
     T1.name= T2.nam` AND 
     T1.phone_number= T2.phone_number AND T1.address= T2.address 
WHERE T2.id <> T1.id 
+0

感謝您的信息,我已編輯。 –

1

請運行下面的查詢,(考慮表名是 「data」),以獲得期望的結果如下:

SELECT * FROM data where name IN (SELECT name FROM data GROUP BY name HAVING COUNT(*) > 1); 
2

這可以通過使用窗口函數來避免在彙總數據上加入並且通常是更快的方式來完成:

select * 
from (
    select id,name,phone_number,address 
     count(*) over (partition by name,phone_number,address) as cnt 
    from the_table 
) t 
where cnt > 1;