如何使用两个表IN SQL在一个表中提高值?

时间:2017-03-06 13:33:14

标签: sql sql-server

如何以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;

1 个答案:

答案 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'
                 );