2012-07-27 58 views
19

選擇,並僅顯示重複的記錄這個問題是非常簡單的我由於某種原因不能得到正確的結果,只顯示重複記錄在MySQL

Table : Paypal_ipn_orders 
id        payer_email 
1        [email protected] 
2        [email protected] 
3        [email protected] 
4        [email protected] 
5        [email protected] 

SELECT id, COUNT(payer_email) `tot` 
FROM paypal_ipn_orders 
GROUP BY payer_email 
HAVING `tot` >1 

樣本輸出

id  tot 
1   2 
4   2 

預期輸出

id  payer_email 
1  [email protected] 
3  [email protected] 
4  [email protected] 
5  [email protected] 

我該如何做到這一點?

回答

39
SELECT id, payer_email 
FROM paypal_ipn_orders 
WHERE payer_email IN (
    SELECT payer_email 
    FROM paypal_ipn_orders 
    GROUP BY payer_email 
    HAVING COUNT(id) > 1 
) 

sqlfiddle

+0

返回這個 '未知表狀態:TABLE_TYPE 未知的表狀態:TABLE_TYPE' 和'顯示行0 - -1(總計0,查詢花了39.6457秒)' – user1542036 2012-07-30 03:32:58

+0

@ user1542036這與此查詢無關。它是正確的(檢查sqlfidddle)。對您的錯誤進行Google搜索。 – 2012-07-30 03:41:41

+0

我發現的唯一情況是它似乎是與特定版本的phpmyadmin的問題?我正在運行3.4.1,所以升級到最新版本應該修復它? – user1542036 2012-07-30 04:09:14

-2

嘗試此查詢:

SELECT id, COUNT(payer_email) `tot` 
FROM paypal_ipn_orders 
GROUP BY id 
HAVING `tot` >1 

幫助?

0
SELECT id, payer_email FROM paypal_ipn_orders 
WHERE payer_email IN (
    SELECT payer_email FROM papypal_ipn_orders GROUP BY payer_email HAVING COUNT(*) > 1) 
+1

這也返回samething爲他人 – user1542036 2012-07-30 03:38:26

8

IN是在我的處境太慢(180秒)

所以我用一個JOIN代替(0.3秒)

SELECT id, payer_email 
FROM paypal_ipn_orders i 
INNER JOIN (
SELECT payer_email 
    FROM paypal_ipn_orders 
    GROUP BY payer_email 
    HAVING COUNT(id) > 1 
) j ON i.payer_email=j.payer_email 
+0

使用 SELECT i.id,i.payer_email FROM paypal_ipn_orders我 否則會顯示不明確的錯誤 – 2016-05-27 12:05:03

2

得到一個表中所有重複行的列表:

如果我要選擇一個或多個列的值是不相同的,也可以是相同的兩個行數據

對於防爆

Select * from TABLE1 where PRIMARY_KEY_COLUMN NOT IN (SELECT PRIMARY_KEY_COLUMN 
FROM TABLE1 
GROUP BY DUP_COLUMN_NAME having (count(*) >= 1)) 
+1

迄今爲止最快的,但你需要從數刪除「=」 (*)> = 1,因爲它將採用僅存在一次的ID。 – Incognito 2014-09-11 17:16:00

1

嗨以上答案都不行。我想選擇用戶名,出生日期也。但在數據庫中,用戶名不重複,但出生日期將重複,則此解決方案將無法工作。

對於這種使用該解決方案 需要採取自連接在同一個表/

SELECT 
    distinct(p1.id), p1.payer_email , p1.username, p1.birth_date 

FROM 
    paypal_ipn_orders AS p1 

INNER JOIN paypal_ipn_orders AS p2 

ON p1.payer_email=p2.payer_email 

WHERE 

p1.birth_date=p2.birth_date 

上面的查詢將返回具有相同EMAIL_ID和相同的出生日期

0

這個工程最快的爲我

所有記錄
SELECT 
    primary_key 
FROM 
    table_name 
WHERE 
    primary_key NOT IN (
     SELECT 
      primary_key 
     FROM 
      table_name 
     GROUP BY 
      column_name 
     HAVING 
      COUNT(*) = 1 
    ); 
0

我認爲這種方式更簡單。輸出顯示付款人的電子郵件在此表中的多個記錄中的id和付款人的電子郵件。結果按ID排序。

SELECT id, payer_email 
    FROM paypal_ipn_orders 
    WHERE COUNT(payer_email)>1 
    SORT BY id; 
1

這裏是一個簡單的例子:

select <duplicate_column_name> from <table_name> group by <duplicate_column_name> having count(*)>=2 

它將definitly工作。:)

0

類似this answer,雖然我用了一個臨時表來代替:

CREATE TEMPORARY TABLE duplicates (
    SELECT payer_email 
    FROM paypal_ipn_orders 
    GROUP BY payer_email 
    HAVING COUNT(id) > 1 
); 
SELECT id, payer_email 
FROM paypal_ipn_orders AS p 
INNER JOIN duplicates AS d ON d.payer_email=p.payer_email; 
1

使用此代碼

SELECT * 
    FROM paypal_ipn_orders 
    GROUP BY payer_email 
    HAVING COUNT(payer_email) >1 
0

SELECT * FROM table T1加入table T2 WHERE(t1.name = T2 .name)& &(t1.id!= t2.id)

+1

你能解釋一下你解決問題的方法嗎?您還可以使用編輯器的代碼縮進功能突出顯示SQL(四個空格)。 – 2016-04-27 15:12:50