2016-04-21 33 views
0

我想刪除主題字段中的相同值並保留第一行的值。如何在Mysql中刪除相同的值

no topic 

1 1234 

2 1234 

3 1234 

沒有=自動增量

輸出

no topic 

1 1234 

這是我的代碼

$sql ="DELETE FROM data 
WHERE no IN (SELECT * 
      FROM (SELECT no FROM data 
        GROUP BY topic HAVING (COUNT(*) > 1) 
       ) AS A 
      )"; 

此代碼刪除第一個值,但我想刪除所有相同的值並保持第一個值,如示例。

回答

1

試試這個

DELETE FROM data 
WHERE no NOT IN (SELECT no FROM 
        (SELECT MIN(no) as no,topic FROM data 
        GROUP BY topic 
        )NotDelete 
       ); 

sqlfiddle

+0

謝謝你對我來說很大部分代碼工作。 – sammy

相關問題