如何以2%的价格适当提高饮料类产品的价格。 这些表位于https://northwinddatabase.codeplex.com/ Northwind(SQL)数据库
中我的非工作查询:
UPDATE products
SET unitprice = unitprice * 1.02
FROM products, categories
WHERE categories.categoryID = products.categoriesID
AND categories.categoryname LIKE '%Beverages'
GROUP BY products.categoryID;
答案 0 :(得分:0)
不需要GROUP BY
:
UPDATE p
SET price = price * 1.02
FROM products p JOIN
categories c
ON c.categoryID = p.categoriesID
WHERE c.description LIKE '%Beverages';
如果多个类别可以匹配单个产品,则上述内容仍然只会将值增加2%(而不是每次匹配2%)。不过,我建议EXISTS
而不是JOIN
:
UPDATE p
SET price = price * 1.02
FROM products p JOIN
WHERE EXISTS (SELECT 1
FROM categories c
WHERE c.categoryID = p.categoriesID AND
c.description LIKE '%Beverages'
);