繁体   English   中英

如何使用 SELECT SUM() FROM Table GROUP BY 删除

[英]How to DELETE With SELECT SUM() FROM Table GROUP BY

我有桌子材料

 - id int,
 - name varchar(50),
 - content text,
 - quantity double,

不使用 group by 的查询

1, product1, content1, 25
2, product2, content2, 4 
3, product1, content3, 35
4, product3, content4, 15 

group by 和 SUM 数量的查询

 product1, content1, SUM(quantity) : 60
 product2, content2, SUM(quantity) : 4 
 product3, content4, SUM(quantity) : 15

例如,如果我的总和数量 = 0

 product1, content1, SUM(quantity) : 0
 product2, content2, SUM(quantity) : 4 
 product3, content3, SUM(quantity) : 15

我将删除产生总数量 = 0 的所有产品。例如:从 id =1 中删除产品 1,从 id = 3 中删除产品 1

1, product1, content1 
3, product1, content3

你可以这样做:

DELETE t
FROM `material` t
INNER JOIN(SELECT s.name FROM `material` s
           GROUP BY s.name
           HAVING sum(s.quantity) = 0)
 ON(t.name = s.name)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM