Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a single table based on data from multiple tables SQL Server 2005,2008

I need to update table one using data from table two. Table one and two are not related by any common column(s). Table three is related to table two.

Ex : table one(reg_det table)

reg_det_id | reg_id | results
101        | 11     | 344

table two :(temp table)

venue                    | results
Anheim convention center | 355

Table three (regmaster-tbl)

reg_id| venue
11    | Anaheim convention center

I need to update results column in table one using data from table two. But table one and two are not related. Table two and three and table one and three are related as you can see above. Can anyone please suggest any ideas! I need the results value to be 355 in table one and this data is coming from table 2, but these two are unrelated, and they can be related using table three. Sorry if it is confusing!

like image 583
user1984533 Avatar asked May 16 '13 20:05

user1984533


People also ask

How do you update a table based on values from another table?

In this article, we will see, how to update from one table to another table based on ID match. We can update the table using UPDATE statement in SQL. The update statement is always followed by the SET command. The SET command is used to specify which columns and values need to be updated in a table.

Can you update multiple tables in SQL in a single query?

2. SQL SERVER: In SQL Server, we can join two or more tables, but we cannot update the data of multiple tables in a single UPDATE statement. So, we need an individual UPDATE query to update each table.

Is it possible to update two tables using join in a single update statement?

For instance, updating 2 different tables together in a single query/statement. This involves the use of the BEGIN TRANSACTION clause and the COMMIT clause. The individual UPDATE clauses are written in between the former ones to execute both the updates simultaneously.

Can you modify the rows in a table based on values from another table?

You can update an entire row in one table with values from a row in another table. Suppose that a master class schedule table needs to be updated with changes that have been made in a copy of the table. The changes are made to the work copy and merged into the master table every night.


2 Answers

Fairly straight forward:

UPDATE T1
SET result = t2.results
FROM [table one] T1
INNER JOIN [table three] t3
 on t1.reg_id = t3.reg_id
INNER JOIN [table two] T2
 on t2.venue = t3.venue
like image 154
MBulava Avatar answered Nov 16 '22 00:11

MBulava


Almost a question instead of an answer. :)

Couldn't you use an implied inner join?

UPDATE rd
   SET rd.results = tt.results
  FROM reg_det rd, regmaster rm, temptable tt
 WHERE rm.reg_id = rd.reg_id
   AND rm.venue = tt.venue;

I find it easier to read, and this syntax works in a SELECT statement, with the same meaning as an explicit inner join.

like image 20
Jesse Chisholm Avatar answered Nov 16 '22 01:11

Jesse Chisholm