如何使用group by删除两列中的重复行?

时间:2019-04-26 19:29:35

标签: mysql sql mariadb

我正在尝试使用此查询删除重复的行,但MySQL不返回任何内容并崩溃:

DELETE FROM project_category WHERE prc_id IN (SELECT prc_id
FROM project_category
GROUP BY prc_proid, prc_catid
HAVING COUNT(*) > 1)

我要删除重复项:

+--------+-----------+-----------+
| prc_id | prc_proid | prc_catid |
+--------+-----------+-----------+
|   1691 |       207 |        16 |
|   1692 |       207 |        16 |
+--------+-----------+-----------+

1 个答案:

答案 0 :(得分:1)

MySql不允许直接引用WHERE子句中发生DELETE的表。这样做:

DELETE FROM project_category 
WHERE prc_id IN (
  SELECT prc_id FROM (
    SELECT prc_id
    FROM project_category
    GROUP BY prc_proid, prc_catid
    HAVING COUNT(*) > 1
  ) t
)