Solved

update based in select

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

688 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