sql/coldfusion delete duplicate rows

北战南征 提交于 2019-12-25 08:35:42

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!