使用SELECT FROM Table GROUP BY进行更新

时间:2016-06-01 09:26:56

标签: mysql sql

我有桌面材料:

- id: int,
 - name : varchar(100),
 - content: varchar(255),
 - quantity : double,

不使用group by

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

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

 product1, content1, 25
 product2, content2, 4 
 product3, content4, 15

当我想要更新第一个id:1,product1,content1,25的数量= 0时,我会为所有具有相同材质名称的材料更新数量= 0 例如:

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

我尝试过:

Update 'material' as m  
INNER JOIN  (SELECT i1.produit FROM 'material' m1   GROUP BY m1.name) m1   
ON(m1.name = m.name) SET quantity = 0

但我没有结果:

2 个答案:

答案 0 :(得分:0)

尝试以下内容;)

UPDATE `material` AS m  
INNER JOIN (
    SELECT m1.name
    FROM `material` m1
    GROUP BY m1.name
    HAVING COUNT(m1.name) > 1) m2   
ON m2.name = m.name
SET m.quantity = 0

答案 1 :(得分:0)

@Reno,这是第一个id的查询 但是当我选择任何身份证时 例如,我会选择第二个id或任何id 不一定是prermier id。 例如,当我选择第二个id时: 我会dipaly

<h4> some text here</h4>

例如当我选择第四个id时: 我会dipaly

1, product1, content1, 10
2, product2, content2, 0
3, product1, content3, 20
4, product3, content4, 15 
5, product2, content5, 0
相关问题