Postgresql更新加入

时间:2018-03-28 09:13:04

标签: sql postgresql join sql-update

我想加入2个表并在第2个表的指定值上更新firts表的值。我没有使用其他解决方案。

UPDATE customers
SET cutoffstop = cutoffstop + '432000'
FROM customers as c
JOIN bluemedia as b ON c.id = b.customerid
WHERE b.orderid = '217201807'

3 个答案:

答案 0 :(得分:5)

常规更新语法:

[ WITH [ RECURSIVE ] with_query [, ...] ]
UPDATE [ ONLY ] table [ [ AS ] alias ]
    SET { column = { expression | DEFAULT } |
          ( column [, ...] ) = ( { expression | DEFAULT } [, ...] ) } [, ...]
    [ FROM from_list ]
    [ WHERE condition | WHERE CURRENT OF cursor_name ]
    [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]

解决问题的方法:

UPDATE customers AS c
SET cutoffstop = cutoffstop + 432000
FROM bluemedia as b
WHERE c.id = b.customerid
AND b.orderid = '217201807'

有关UPDATE语法的详细信息,请访问以下链接:

  

https://www.postgresql.org/docs/current/static/sql-update.html

答案 1 :(得分:0)

  

使用此查询进行更新:

UPDATE customers
SET cutoffstop = cutoffstop + 432000
FROM bluemedia as b ON customers.id = b.customerid
WHERE b.orderid = '217201807'

答案 2 :(得分:0)

您可以使用EXISTS

UPDATE customers c
SET c.cutoffstop = c.cutoffstop + '432000'
WHERE EXISTS (
        SELECT 1
        FROM bluemedia b
        WHERE c.id = b.customerid
            AND b.orderid = '217201807'
        );