Link to home
Start Free TrialLog in
Avatar of Ingo Foerster
Ingo Foerster

asked on

MySqL Update value from Select

How I can I update a value I got from an Select query?

Sample: Update mytable1 Set my mytable1.value = (Select value from mytable2 where mytable2.id=1) where mytable1.id=1
And do not want to select. put in to a variable and write back to the db.

I use a MySQL Database on Linux.
Avatar of Apurva Bhandari
Apurva Bhandari
Flag of India image

What is the exact requirement ?
You want shell script which will update mytable as mytable2 data or something else??
Avatar of johnsone
Why do you want to select something out of the database and then put it back?  Do all the work in the database.

FYI - I think your update should be:
UPDATE mytable1 
SET    my mytable1.value = 
       ( 
              SELECT value 
              FROM   mytable2 
              WHERE  mytable2.id=mytable1.id) 
WHERE  mytable1.id=1

Open in new window

You should use correlated subqueries.
do you have common overlap?

update table2 join table1 on table1.column1=tablet2.columna set table1.column2=table2.columnb where condition.


Seems like duplicative information,
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.