SQL: how to independently refer to a column in update operations?

I have two tables with these columns:

Users: user, class, class, location.

Classes: class, location

The class column of the Users table shows the same column in the Classes table.

I want to update all rows in the Users table so that each row of the "location" column of the "Users" table is equal to the location of the class.

So, I have a line with the values: Mike, Math, A +, New York

And the corresponding row in the Classes table for Math: Mathematics, Chicago

I want the user table row to become Mike, Math, A +, Chicago.

thanks

+4
source share
3 answers
update users a set a.location = (select b.location from classes b where b.class = a.class) 
+1
source
 update users set users.location = classes.location from classes where classes.class = users.class 
+3
source

How about this:

 UPDATE U SET U.Location = C.Location FROM Users AS U INNER JOIN Classes AS C ON C.Class = U.Class AND C.Location != U.Location ; 
+2
source

Source: https://habr.com/ru/post/1382362/


All Articles