我有一个iOS应用程序,用于同步来自Web服务的数据。每隔一段时间,记录就会重复,我需要能够清除重复记录。
我可以使用以下SQL轻松检查重复:
SELECT ZOrderGUID, COUNT(*) AS Cnt
FROM ZPurchaseOrder
WHERE ZManufacturerID=18
GROUP BY ZOrderGUID
HAVING Count(*) > 1
我需要能够使用这些结果来删除欺骗 - 通常只有2条记录。
我试过运行这个SQL:
SELECT LIMIT 1 * FROM ZPurchaseorder WHERE ZManufacturerID=18 AND ZOrderGUID=
(SELECT ZOrderGUID
FROM ZPurchaseOrder
WHERE ZManufacturerID=18
GROUP BY ZOrderGUID
HAVING Count(*) > 1)
为了隔离我发现的第一个欺骗,但在测试中它锁定了我用来测试我的SQL脚本的SQLite数据库浏览器应用程序。
答案 0 :(得分:0)
你可以选择这样的第一条记录(LIMIT
出现在查询的末尾):
SELECT *
FROM ZPurchaseorder
WHERE ZManufacturerID=18
AND ZOrderGUID = (SELECT ZOrderGUID
FROM ZPurchaseOrder
WHERE ZManufacturerID=18
GROUP BY ZOrderGUID
HAVING Count(*) > 1)
LIMIT 1