根据条件更新声明

时间:2017-10-28 12:07:07

标签: sql postgresql phppgadmin

如何更新DB中的discount_price 我需要根据我的条件为现有价值增加价值 示例:任何产品的价格从10到30 我必须加1.5。

15的产品应该是16.5例如

我写了这个查询

UPDATE products
    SET discount_price = discount_price + 1.5 
    WHERE discount_price >= 10 AND discount_price <= 30;

但我发现此错误&gt;&gt;

SQL错误:

ERROR:  syntax error at or near "SET"
LINE 1: SELECT COUNT(*) AS total FROM (UPDATE products SET discount_...
                                                       ^
In statement:
SELECT COUNT(*) AS total FROM (UPDATE products SET discount_price = discount_price + 1.5 
WHERE discount_price >= 10 AND discount_price <=30) AS sub

enter image description here

1 个答案:

答案 0 :(得分:0)

我猜您正在使用Postgres并希望计算受更新影响的行数。如果是这样,您可以使用可更新的CTE:

WITH p as (
      UPDATE products
          SET discount_price = discount_price + 1.5 
          WHERE discount_price >= 10 AND discount_price <= 30
          RETURNING *
     )
SELECT COUNT(*)
FROM p;

MySQL中没有等效的语法。