使用 mysql 查询删除价格较低的重复产品

Remove duplicate product with lower price using mysql query

我有很多重复的产品需要删除。是否有删除这些 "Duplicates" 和 "lower price" and/or "Same Price" 并且每个产品只保留 1 个的查询?

重复的有重复的"Product Name"我使用的是Opencart 2.1.0.1版本

没有太多信息(哪个 RDBMS 等),我只能推测此解决方案对您有用:

WITH Flagged AS
(
SELECT
    ProductName,
    CASE WHEN ROW_NUMBER() OVER(PARTITION BY ProductName ORDER BY Price DESC) = 1 THEN 0 ELSE 1 END AS Delete
FROM
    Products
)

DELETE p
FROM Products p
JOIN Flagged f
ON (p.ProductName = f.ProductName)
WHERE f.Delete = 1;

由于您正在使用mysql,您需要使用连接(不支持分区):

select:

select p.* 
from products as p 
join 
(
    select name, min(price) as price 
    from products group by name having count(price) = 2
) as p2 on p2.name = p.name and p2.price = p.price;

获取所有重复产品的最低价格(其中重复假定同一产品恰好有两行)。

要删除,把开头的select改成删除,如下:

delete p.* 
from products as p 
join 
(
    select name, min(price) as price 
    from products group by name having count(price) = 2
) as p2 on p2.name = p.name and p2.price = p.price;

根据 http://wiki.opencarthelp.com/doku.php?id=databse_schema 上的架构和只有一种语言,以下查询应该可以解决您的问题:

delete p1 
from product p1
join product_description d1 on d1.product_id = p1.product_id
join product_description d2 
  on  d2.product_id <> d1.product_id
  and d2.language_id = d1.language_id
  and d2.name = d1.name
join product p2 on p2.product_id = d2.product_id
where d1.language_id = 1 -- define the language used for product name
  and (p2.price > p1.price -- delete if higher price exists
    or p2.price = p1.price and p2.product_id < p1.product_id -- delete if same price with lower id exists
  )
;