How to do 3 table JOIN in UPDATE query?

后端 未结 6 2046
走了就别回头了
走了就别回头了 2020-11-21 07:00

I asked a question and got this reply which helped.

   UPDATE TABLE_A a JOIN TABLE_B b 
   ON a.join_col = b.join_col AND a.column_a = b.column_b 
   SET a.c         


        
相关标签:
6条回答
  • 2020-11-21 07:54

    Yes, you can do a 3 table join for an update statement. Here is an example :

        UPDATE customer_table c 
    
          JOIN  
              employee_table e
              ON c.city_id = e.city_id  
          JOIN 
              anyother_ table a
              ON a.someID = e.someID
    
        SET c.active = "Yes"
    
        WHERE c.city = "New york";
    
    0 讨论(0)
  • 2020-11-21 07:57

    the answer is yes you can

    try it like that

    UPDATE TABLE_A a 
        JOIN TABLE_B b ON a.join_col = b.join_col AND a.column_a = b.column_b 
        JOIN TABLE_C c ON [condition]
    SET a.column_c = a.column_c + 1
    

    EDIT:

    For general Update join :

       UPDATE TABLEA a 
       JOIN TABLEB b ON a.join_colA = b.join_colB  
       SET a.columnToUpdate = [something]
    
    0 讨论(0)
  • 2020-11-21 07:59

    Below is the Update query which includes JOIN & WHERE both. Same way we can use multiple join/where clause, Hope it will help you :-

    UPDATE opportunities_cstm oc JOIN opportunities o ON oc.id_c = o.id
     SET oc.forecast_stage_c = 'APX'
     WHERE o.deleted = 0
       AND o.sales_stage IN('ABC','PQR','XYZ')
    
    0 讨论(0)
  • 2020-11-21 07:59

    An alternative General Plan, which I'm only adding as an independent Answer because the blasted "comment on an answer" won't take newlines without posting the entire edit, even though it isn't finished yet.

    UPDATE table A
    JOIN table B ON {join fields}
    JOIN table C ON {join fields}
    JOIN {as many tables as you need}
    SET A.column = {expression}
    

    Example:

    UPDATE person P
    JOIN address A ON P.home_address_id = A.id
    JOIN city C ON A.city_id = C.id
    SET P.home_zip = C.zipcode;
    
    0 讨论(0)
  • 2020-11-21 08:00

    Alternative way of achieving same result is not to use JOIN keyword at all.

    UPDATE TABLE_A, TABLE_B
    SET TABLE_A.column_c = TABLE_B.column_c + 1
    WHERE TABLE_A.join_col = TABLE_B.join_col
    
    0 讨论(0)
  • 2020-11-21 08:01

    For PostgreSQL example:

    UPDATE TableA AS a
    SET param_from_table_a=FALSE -- param FROM TableA
    FROM TableB AS b
    WHERE b.id=a.param_id AND a.amount <> 0; 
    
    0 讨论(0)
提交回复
热议问题