Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update from select with multiple fields?

Tags:

select

mysql

I can do the following to insert records into a table from a select on another table:

INSERT INTO table (field1, field2) SELECT field1,field2 FROM table2

Can I do the same with an update ?? Something like this (not working!):

UPDATE table SET field1=table2.field1, field2=table2.field2 SELECT field1,field2 FROM table2
WHERE table.field0=table2.field0

I know how to do this with only 1 field, but is there a way to do it with multiple fields?

like image 578
Dylan Avatar asked Jul 22 '11 20:07

Dylan


People also ask

Can we UPDATE multiple columns in a single UPDATE statement?

We can update multiple columns by specifying multiple columns after the SET command in the UPDATE statement. The UPDATE statement is always followed by the SET command, it specifies the column where the update is required.

How UPDATE multiple columns with different values in SQL?

To update multiple columns use the SET clause to specify additional columns. Just like with the single columns you specify a column and its new value, then another set of column and values. In this case each column is separated with a column.

Do UPDATE Set multiple columns?

The UPDATE statement in SQL is used to update the data of an existing table in database. We can update single columns as well as multiple columns using UPDATE statement as per our requirement. UPDATE table_name SET column1 = value1, column2 = value2,...


1 Answers

UPDATE table A INNER JOIN table2 B USING (field0)
SET A.field1 = B.field1,A.field2 = B.field2;
like image 136
RolandoMySQLDBA Avatar answered Sep 17 '22 22:09

RolandoMySQLDBA