[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

update based in select

Posted on 2013-01-29
3
Medium Priority
?
323 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
[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
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

649 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