使用GROUP BY删除重复项的查询

时间:2013-10-02 21:04:14

标签: mysql sql duplicates

id_specific_price    id_product  
-------------------------------
            1                2  
            2                2  
            3                2  
            4                3  
            5                3  
            6                3  
            7                3

需要删除重复项,预期结果:

id_specific_price    id_product  
-------------------------------
            3                2  
            7                3

SELECT * 
  FROM ps_specific_price 
 WHERE id_specific_price NOT IN 
 (SELECT MAX(id_specific_price) 
    FROM ps_specific_price 
   GROUP BY id_product) 

有效但

DELETE FROM ps_specific_price 
 WHERE id_specific_price NOT IN 
(SELECT MAX(id_specific_price) 
   FROM ps_specific_price 
  GROUP BY id_product)

没有。有很多例子可以解决这个问题,但由于某种原因,我无法适应它。我相信它是GROUP BY。例如:

DELETE FROM ps_specific_price 
 WHERE id_specific_price NOT IN
 (SELECT MAX(p.id_specific_price) 
    FROM (SELECT * FROM ps_specific_price ) as p)
   GROUP BY id_product

我在哪里出错?

3 个答案:

答案 0 :(得分:11)

如果您正在寻找MySQL的解决方案,那么您可以使用正确的multi table DELETE语法以及JOIN这样的

DELETE p
  FROM ps_specific_price p JOIN
(
  SELECT id_product, MAX(id_specific_price) id_specific_price
    FROM ps_specific_price
   GROUP BY id_product
) d 
   ON p.id_product = d.id_product
  AND p.id_specific_price <> d.id_specific_price;

结果:

| ID_SPECIFIC_PRICE | ID_PRODUCT |
|-------------------|------------|
|                 3 |          2 |
|                 7 |          3 |

这是 SQLFiddle 演示

答案 1 :(得分:1)

试试这个:

CREATE TABLE ps_specific_price (
  id_specific_price NUMBER,
  id_product NUMBER
);

INSERT INTO ps_specific_price (id_specific_price, id_product) VALUES (1, 2);
INSERT INTO ps_specific_price (id_specific_price, id_product) VALUES (2, 2);
INSERT INTO ps_specific_price (id_specific_price, id_product) VALUES (3, 2);
INSERT INTO ps_specific_price (id_specific_price, id_product) VALUES (4, 3);
INSERT INTO ps_specific_price (id_specific_price, id_product) VALUES (5, 3);
INSERT INTO ps_specific_price (id_specific_price, id_product) VALUES (6, 3);
INSERT INTO ps_specific_price (id_specific_price, id_product) VALUES (7, 3);

COMMIT;

DELETE FROM ps_specific_price ps
  WHERE ps.id_specific_price NOT IN (
    SELECT MAX(id_specific_price)
      FROM ps_specific_price ps_in
    WHERE ps_in.id_product = ps.id_product
    );

SELECT * FROM ps_specific_price;

ID_SPECIFIC_PRICE      ID_PRODUCT             
---------------------- ---------------------- 
3                      2                      
7                      3                      

您必须将内部查询中的表与外部查询中的表连接起来。

我正在使用Oracle 11g R2。我在SQLFiddle上检查了这个并且我的DELETE语句对MySQL无效 - 没有安装那个并且没有太多经验,但你没有说你正在使用什么数据库。

答案 2 :(得分:0)

可能你想尝试:

DELETE FROM ps_specific_price WHERE(id_product,id_specific_price)NOT IN(SELECT id_product,MAX(id_specific_price)FROM ps_specific_price GROUP BY id_product);

我在Teradata数据库上尝试了这个,但它确实有效。

在您之前的查询中,我看到您唯一遗漏的是映射id_product及其最高价格 删除设置。删除时不会知道它必须在删除之前匹配pid和价格。

希望这会有所帮助。