Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update with join statement mysql?

Tags:

mysql

Is this possible in mysql?

update table1 
set column1 = (select column1 from table2  
               inner join table3 
               where table2.column5 = table3.column6);

Here is a a similar question for an Oracle DB.

like image 698
Bart Avatar asked Oct 01 '10 02:10

Bart


People also ask

Can we use join in update statement?

The most easiest and common way is to use join clause in the update statement and use multiple tables in the update statement. Here we can see that using join clause in update statement. We have merged two tables by the use of join clause.

Can we use inner join in update statement MySQL?

The MySQL Update Join is used for executing the update statement together with the implementation of INNER JOIN and LEFT JOIN MySQL clauses in the server. This Update JOIN clause in MySQL helps to retrieve the data records from the related database tables along with modifying them with the query.

Can we join two tables in update query?

It is possible to join two or more tables in an UPDATE query.

How do I update inner join query in SQL?

SQL UPDATE JOIN could be used to update one table using another table and join condition. UPDATE tablename INNER JOIN tablename ON tablename. columnname = tablename.


1 Answers

You can do it. However, in the example you give, there's no JOIN connection between table1 and the source of the values for the update (table2 INNER JOIN table3), so the results will be somewhat unpredictable.

Your query would be something like (I'm not a MySQL expert):

UPDATE table1, table2, table3 SET table1.column1 = table2.column1
    WHERE table2.column5 = table3.column6

but what I think you probably want (I'm just guessing) is something more like:

UPDATE table1, table2, table3 SET table1.column1 = table2.column1
    WHERE table1.somecolumn = table3.somecolumn AND table2.column5 = table3.column6
like image 184
Larry Lustig Avatar answered Oct 19 '22 04:10

Larry Lustig