删除具有条件的列中的元组

时间:2014-08-29 21:34:01

标签: mysql duplicates

我在同一类别(在COL1中定义)中有一个带有重复项(COL2)的表,当我尝试删除COL2中的元组时,我找不到在同一类别中执行此操作的方法,而是删除它表格中到处都是元组。

Here is the current table T1 BEFORE removing duplicates:

        COL1      COL2
         A         1
         A         2
         A         2
         A         2
         B         1
         B         2
         B         2
         B         3


Here is the current table T1 AFTER removing duplicates:

        COL1      COL2
         A         1
         A         2
         B         3

Here is what I should have in T1:
        COL1      COL2
         A         1
         A         2
         B         1
         B         2
         B         3

Here is the code I used to delete duplicates:

    CREATE TABLE TEMP LIKE T1;
    INSERT INTO TEMP SELECT * FROM T1 WHERE 1 GROUP BY COL2;
    DROP TABLE T1;
    ALTER TABLE TEMP RENAME T1;

知道如何告诉mysql只在COL1类别中删除元组吗?

1 个答案:

答案 0 :(得分:1)

GROUP BY子句中使用这两列:

INSERT INTO TEMP
SELECT *
FROM T1
GROUP BY COL1, COL2