Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an inner join with subqueries in an update syntax

Tags:

sql

sql-server

I am trying to use a inner join with an update statement with a subquery ... can you help me out with the sytax please --- and also how do you use the AS clause for alias in sql server??? the following is what i am trying to do :

Update Table1 
inner join table2
set table1.value1 = (select table2.value1 where table1.value 1 ....)

any idea??

like image 752
AltF4_ Avatar asked May 14 '26 20:05

AltF4_


1 Answers

If you need to use a subquery to perform the UPDATE you can do it this way:

UPDATE t1
SET t1.value = t2.value
FROM Table1 t1
JOIN
(
    SELECT id, value
    FROM table2
) t2
ON t1.id = t2.id
like image 186
Taryn Avatar answered May 17 '26 11:05

Taryn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!