sql / coldfusion删除重复的行

时间:2012-04-23 22:10:59

标签: sql coldfusion

这是我的表

Display          UPC
0                0553406259120
0                0753406259120
1                0753406259120
1                0453406259120

如果您注意到,第2行和第3行具有相同的UPC。我想删除所有显示= 0和重复upc的行。所以在我的表中我只想删除第2行。到目前为止,这是我的coldfusion代码无效。请指教。

<cfquery name="GetData" datasource="#Application.ds#" dbtype="ODBC" username="#Application.UserName#" password="#Application.Password#">
DELETE UPC
FROM products
WHERE DISPLAY = 0
GROUP BY UPC  
HAVING COUNT(*)>1
</cfquery>

2 个答案:

答案 0 :(得分:3)

假设您要删除UPC相同的所有行,但Display不同:

DELETE FROM Products as a
WHERE display = 0
AND EXISTS (SELECT '1'
            FROM Products as b
            WHERE b.display <> 0
            AND b.upc = a.upc)

这应该适用于所有RDBMS,并将删除UPC相同但Display代码不同的所有行。

答案 1 :(得分:0)

DELETE FROM Products
WHERE DISPLAY = 0 
AND UPC in (SELECT UPC
            FROM Products
            GROUP BY UPC
            HAVING COUNT(UPC)>1)