问题
Here is my table
Display UPC 0 0553406259120 0 0753406259120 1 0753406259120 1 0453406259120
If you notice, row 2 and 3 have the same UPC. I would like to delete all rows that have display = 0 and duplicate upc. So in my table I want to delete row 2 only. Here is my coldfusion code so far that doesn't work. Please advice.
<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>
回答1:
Assuming that you want to delete all rows where UPC is the same, but Display is different:
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)
This should work on all RDBMSs, and will remove all rows where UPC is the same, but with a different Display codes.
回答2:
DELETE FROM Products
WHERE DISPLAY = 0
AND UPC in (SELECT UPC
FROM Products
GROUP BY UPC
HAVING COUNT(UPC)>1)
来源:https://stackoverflow.com/questions/10289032/sql-coldfusion-delete-duplicate-rows