我有桌面材料
- 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
具有分组依据和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
答案 0 :(得分:1)
你可以这样做:
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)