update join sql code example

Example 1: inner join update

UPDATE 
    t1
SET 
    t1.c1 = t2.c2,
    t1.c2 = expression,
    ...   
FROM 
    t1
    [INNER | LEFT] JOIN t2 ON join_predicate
WHERE 
    where_predicate;

Example 2: update with join

UPDATE
        first_table ft
        JOIN second_table st ON st.some_id = ft.some_id
        JOIN third_table tt  ON tt.some_id = st.some_id
        .....
    SET
        ft.some_column = some_value
    WHERE ft.some_column = 123456 AND st.some_column = 123456

Example 3: update with join sql server

UPDATE 
    t1
SET 
    t1.c1 = t2.c2,
    t1.c2 = expression,
    ...   
FROM 
    t1
    [INNER | LEFT] JOIN t2 ON join_predicate
WHERE 
    where_predicate;

Example 4: joins in update query

update a
join b 
on a.a_id=b.b_id 
set b.is_active = 1 
where ....;

Example 5: sql update with field from another table

UPDATE table1 
   SET price=(SELECT price FROM table2 WHERE table1.id=table2.id);

Tags:

Sql Example