Mysql 使用where子句删除重复项

Mysql 使用where子句删除重复项,mysql,sql,Mysql,Sql,嘿,我有一组结构如下的数据: id product_number product_type 1 1001 car 2 1002 house 但数据有一些重复项,其中: id product_number product_type 1 1001 car 2 1001 house 我需要删除重复项,但只

嘿,我有一组结构如下的数据:

id     product_number      product_type
1      1001                car
2      1002                house
但数据有一些重复项,其中:

id     product_number      product_type
1      1001                car
2      1001                house
我需要删除重复项,但只删除=house的值

在我看来,这个问题应该是这样的:

DELETE *
FROM table
WHERE product_number is duplicate AND product_type = house

谢谢

在MySQL中,你可以通过加入
来做你想做的事

delete t
    from table t join
         (select product, count(*) as cnt
          from table
          group by product
         ) tt
         on tt.product = t.product
    where tt.cnt > 1 and t.product_type = 'house';

并且product_type=house

您可以将更新查询与以下连接一起使用:

delete t1.*
from
  t t1 INNER JOIN t t2
  ON t1.product_number = t2.product_number
     AND t1.product_type='house' AND (
       t2.product_type<>'house'
       OR t1.id>t2.id
     )
删除t1*
从…起
t1内连接t2
在t1上。产品编号=t2。产品编号
和t1.产品类型='house'和(
t2.产品类型“房屋”
或者t1.id>t2.id
)

您有多种方法:

1. 您可以使用以下查询:

INSERT INTO new_table (SELECT DISTINCT * FROM old_table WHERE product_type = house) 
2. 您可以更改表并更改product number列以向其添加索引

ALTER IGNORE TABLE jobs
ADD UNIQUE INDEX idx_name (product_number);
因此,具有相同产品编号的重复行将被自动删除,并进行更多操作

3. 您可以尝试此查询来:

DELETE n1 FROM table_product p1, table_product p2 WHERE p1.product_number = p2.product_number AND p1.product_type= house

重复的产品编号和/或id?您好,是更新的问题,产品编号的副本
DELETE n1 FROM table_product p1, table_product p2 WHERE p1.product_number = p2.product_number AND p1.product_type= house