Solved

update based in select

Posted on 2013-01-29
3
319 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 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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…

696 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