Solved

update based in select

Posted on 2013-01-29
3
309 Views
Last Modified: 2013-01-30
Hello Guys,

I have 2 tables and I need to mount an update based on the results of a select. example:

Table1
name, phone, record, age

Table2
name, phone, record, age

Both tables have the same structure. I would like to check if customers table1 exists in table2 if there is an update to make updating the data from table1 to table2
0
Comment
Question by:eduardo12fox
3 Comments
 
LVL 13

Expert Comment

by:AielloJ
ID: 38833590
eduardo12fox,


INSERT INTO table2 (name, phone, record, age)
SELECT name, phone, record, age
FROM table1
WHERE (name, phone, record, age) NOT IN
       (SELECT name, phone, record, age
        FROM table2);

Best regards,

AielloJ
0
 
LVL 24

Accepted Solution

by:
johanntagle earned 500 total points
ID: 38833765
You want to update if there are common records, not insert new records, right?  Assuming the name column is the unique identifier:

update table2 t2 join table1 t1 on t2.name=t1.name
set t2.phone=t1.phone, t2.record=t1.record, t2.age=t1.age;

If you want to both update existing and insert those in table1 that are not in table2:

1.  Make sure name column is either primary key or has unique index.
2. Execute:

replace into table2 (name, phone, record, age)
select name, phone, record age from table1;

See http://dev.mysql.com/doc/refman/5.5/en/replace.html for more info on the REPLACE keyword.
0
 

Author Closing Comment

by:eduardo12fox
ID: 38834821
Thank you
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now