updating table rows in postgres using subquery

前端 未结 6 1185
北荒
北荒 2020-11-27 08:42

Using postgres 8.4, My goal is to update existing table:

CREATE TABLE public.dummy
(
  address_id SERIAL,
  addr1 character(40),
  addr2 character(40),
  ci         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 09:36

    @Mayur "4.2 [Using query with complex JOIN]" with Common Table Expressions (CTEs) did the trick for me.

    WITH cte AS (
    SELECT e.id, e.postcode
    FROM employees e
    LEFT JOIN locations lc ON lc.postcode=cte.postcode
    WHERE e.id=1
    )
    UPDATE employee_location SET lat=lc.lat, longitude=lc.longi
    FROM cte
    WHERE employee_location.id=cte.id;
    

    Hope this helps... :D

提交回复
热议问题