PostgreSQL - Using a Subquery to Update Multiple Column Values

后端 未结 7 1039
无人共我
无人共我 2021-02-02 08:01

I need to be able to update multiple columns on a table using the result of a subquery. A simple example will look like below -

UPDATE table1
SET (col1, col2) =         


        
7条回答
  •  我在风中等你
    2021-02-02 08:17

    UPDATE table1
    SET
        col1 = subquery.min_value,
        col2 = subquery.max_value
    FROM
    (
    
        SELECT
            1001 AS col4,
            MIN (ship_charge) AS min_value,
            MAX (ship_charge) AS max_value
        FROM orders
    ) AS subquery
    WHERE table1.col4 = subquery.col4
    

    You can also return multiple rows in the subquery if you want to update multiple rows at once in table1.

提交回复
热议问题