Solved

update query

Posted on 2009-05-19
5
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

730 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