How to remove duplicate with certain condition

后端 未结 1 1213
攒了一身酷
攒了一身酷 2021-01-28 16:35

I have a mysql table I want to remove the duplicate with a certain condition

I want to remove if name, cl_1, cl_2, cl_3, cl_4, cl_5, cl_6, are exactly same other wise if

1条回答
  •  囚心锁ツ
    2021-01-28 17:25

    This should work:

    DELETE Y
    FROM YourTable Y JOIN 
       (
      SELECT
        name, cl_1, cl_2, cl_3, cl_4, cl_5, cl_6 
      FROM YourTable
      GROUP BY name, cl_1, cl_2, cl_3, cl_4, cl_5, cl_6
      HAVING COUNT(1) > 1
         ) T ON Y.name = T.name 
            AND Y.cl_1 = T.cl_1
            AND Y.cl_2 = T.cl_2
            AND Y.cl_3 = T.cl_3
            AND Y.cl_4 = T.cl_4
            AND Y.cl_5 = T.cl_5
            AND Y.cl_6 = T.cl_6
    

    SQL Fiddle Demo

    BTW -- What about rows 3 and 6? Those seem to be the exact same?

    http://sqlfiddle.com/#!2/26b8b/1

    0 讨论(0)
提交回复
热议问题