Group_Concat 在子查询中删除重复项
Group_Concat in Subquery to Remove Duplicates
我们的数据库中有几个重复的商店需要删除,我们通过使用商店的纬度来识别重复的商店。
在生产者 table 的这个示例中,我们想要删除生产者 ID 123(我们的生产 Table 有 10K 行)。
+------+--------+------+------+---------+--------+
| producerID | producerName | type | lat |
+------+--------+------+------+---------|--------|
| 123 | ToysRUs | Retail | 52.445 |
| 124 | Toys R Us | Online | 52.445 |
| 234 | GameStop | Retail | 55.150 |
| 345 | Amazon | Online | 56.110 |
+------+--------+------+------+---------+--------|
*Store 与 Producer 相同
此查询为我们提供了具有相同纬度值的所有生产者 ID。
SELECT COUNT(*) AS 'TotalStores', GROUP_CONCAT(producerID, SEPARATOR ',') AS 'DupeStores'
FROM Producer
GROUP BY lat
HAVING TotalStores > 1
ORDER BY TotalStores DESC
+------+--------+------+----+
| TotalStores | DupeStores |
+------+--------+------+----+
| 3 | 123,124,125 |
| 3 | 555,556,557 |
| 2 | 666,665 |
| 2 | 777,787 |
+------+--------+------+----+
然后,我们使用这些生产者 ID (copy/paste) 和 运行 另一个查询来根据类型删除那些重复项。任何具有重复且类型为 'Retail' 的内容,我们都想删除。
DELETE FROM Producer WHERE type = 'Retail'
AND storeID IN (123, 124, 125, 555, 556, 557, 666, 665, 777, 787)
我们如何将这些组合成一个查询?当我尝试这样做时,出现错误:
Operand should contain 1 column(s)
DELETE FROM Producer WHERE type = 'Retail'
AND producerID IN (SELECT COUNT(*) AS 'TotalStores', GROUP_CONCAT(producerID SEPARATOR ',')
FROM Producer
GROUP BY lat
HAVING TotalStores > 1)
在 DELETE
语句中使用自联接:
DELETE p1
FROM Producer p1 INNER JOIN Producer p2
ON p2.lat = p1.lat
WHERE p1.type = 'Retail' AND p2.type <> p1.type
参见demo。
我们的数据库中有几个重复的商店需要删除,我们通过使用商店的纬度来识别重复的商店。
在生产者 table 的这个示例中,我们想要删除生产者 ID 123(我们的生产 Table 有 10K 行)。
+------+--------+------+------+---------+--------+
| producerID | producerName | type | lat |
+------+--------+------+------+---------|--------|
| 123 | ToysRUs | Retail | 52.445 |
| 124 | Toys R Us | Online | 52.445 |
| 234 | GameStop | Retail | 55.150 |
| 345 | Amazon | Online | 56.110 |
+------+--------+------+------+---------+--------|
*Store 与 Producer 相同
此查询为我们提供了具有相同纬度值的所有生产者 ID。
SELECT COUNT(*) AS 'TotalStores', GROUP_CONCAT(producerID, SEPARATOR ',') AS 'DupeStores'
FROM Producer
GROUP BY lat
HAVING TotalStores > 1
ORDER BY TotalStores DESC
+------+--------+------+----+
| TotalStores | DupeStores |
+------+--------+------+----+
| 3 | 123,124,125 |
| 3 | 555,556,557 |
| 2 | 666,665 |
| 2 | 777,787 |
+------+--------+------+----+
然后,我们使用这些生产者 ID (copy/paste) 和 运行 另一个查询来根据类型删除那些重复项。任何具有重复且类型为 'Retail' 的内容,我们都想删除。
DELETE FROM Producer WHERE type = 'Retail'
AND storeID IN (123, 124, 125, 555, 556, 557, 666, 665, 777, 787)
我们如何将这些组合成一个查询?当我尝试这样做时,出现错误:
Operand should contain 1 column(s)
DELETE FROM Producer WHERE type = 'Retail'
AND producerID IN (SELECT COUNT(*) AS 'TotalStores', GROUP_CONCAT(producerID SEPARATOR ',')
FROM Producer
GROUP BY lat
HAVING TotalStores > 1)
在 DELETE
语句中使用自联接:
DELETE p1
FROM Producer p1 INNER JOIN Producer p2
ON p2.lat = p1.lat
WHERE p1.type = 'Retail' AND p2.type <> p1.type
参见demo。