Solved

update query

Posted on 2009-05-19
5
215 Views
Last Modified: 2012-05-07
Hi, I have a table1 in mysql database with two fields - user and completed. completed is an integer. I also have table2 with two fields user and completed. I want to update the completed field in table 1 with the completed field from table2 for a user.

could somebody give me the code.

thanks.
0
Comment
Question by:RupertA
5 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 250 total points
ID: 24422472
update t1
set completed = t2.completed
from table1 t1
join table2 t2 on t1.user = t2.user
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 250 total points
ID: 24422474
This is an article I wrote for SQL Server.  The syntax should be the same for MySQL.

http://sqlservernation.com/blogs/howtos/archive/2009/03/19/updating-values-in-one-table-with-values-from-another-table.aspx

0
 
LVL 20

Expert Comment

by:a_b
ID: 24422479
you need a nested query -

insert into table1 (completed) values(select completed from table2 where id = {id of required record})
0
 

Author Comment

by:RupertA
ID: 24429791
I ended up creating this which worked. your t sql didn't seem to work for me chapmandew :(

update courses
set completed=(select completed from table_bathroom_completed where courses.userref=table_bathroom_completed.userref and courses.courseID="bathrooms") WHERE EXISTS (select completed from table_bathroom_completed where courses.userref=table_bathroom_completed.userref);

However I will share the points out to chapmandew for trying and Brandon for his article.

Thanks.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24430743
Rupert:

The correlated sub-select that you are using should function the same as the update/from that chap and I both provided.  Care to share what "didn't work" about it.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question