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

时间:2021-01-06 00:49:58

I have table material

我有桌子材料

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

the query without using group by

不使用group by的查询

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

the query with group by and SUM quantity

具有分组依据和SUM数量的查询

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

for example, if i have sum quantity = 0

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

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

I would remove all products that produce the sum quantity = 0. for example: remove product 1 from id =1 and product 1 from id = 3

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

1, product1, content1 
3, product1, content3

1 个解决方案

#1


1  

You can do this:

你可以这样做:

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)

#1


1  

You can do this:

你可以这样做:

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)