[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MySQL query to update data from one table to another table

Posted on 2009-04-26
5
Medium Priority
?
2,073 Views
Last Modified: 2012-05-06
update query is needed for MySQL
--
-- Creation script with sample data 
--
 
CREATE TABLE emp (
   id int(9) unsigned NOT NULL auto_increment,
   empno varchar(6) NOT NULL,
   ename varchar(30) NOT NULL,
   address varchar(30),
   mystatus varchar(2), 
   PRIMARY KEY  (id)
) DEFAULT CHARSET=utf8;
 
 
INSERT INTO emp (id , empno, ename, address ,mystatus)
VALUES (1,'123456', 'PKM', 'Address1', 'OK');
 
INSERT INTO emp (id , empno, ename, address ,mystatus)
VALUES (2,'234567', 'KSB', 'Address2', 'OK');
 
INSERT INTO emp (id , empno, ename, address ,mystatus)
VALUES (3,'345678', 'KMM', 'Address3', 'OK');
 
 
CREATE TABLE emp_temp (
   id int(9) unsigned NOT NULL auto_increment,
   empno varchar(6) NOT NULL,
   ename varchar(30) NOT NULL,
   address varchar(30),
   PRIMARY KEY  (id)
) DEFAULT CHARSET=utf8;
 
 
INSERT INTO emp_temp (id , empno, ename, address )
VALUES (1,'123456', 'PKM', 'Add1');
 
INSERT INTO emp_temp (id , empno, ename, address )
VALUES (2,'234567', 'KSB', 'Add2');
 
INSERT INTO emp_temp (id , empno, ename, address )
VALUES (3,'345678', 'KMM', 'Add3');
 
INSERT INTO emp_temp (id , empno, ename, address )
VALUES (4,'456789', 'MMG', 'Add4');
 
 
Here I have two tables emp and emp_temp. 
Now I want to update the table emp from the table data emp_temp. That is the final result in the table emp is
 
1,'123456', 'PKM', 'Add1', 'OK'
2,'234567', 'KSB', 'Add2', 'OK'
3,'345678', 'KMM', 'Add3', 'OK'
4,'456789', 'MMG', 'Add4', ''
 
My friend told me to use ON DUPLICATE KEY UPDATE statement in this case.
Please help. I am novice to MYSQL.

Open in new window

0
Comment
Question by:karunamoorthy
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:joep1978
ID: 24237841
replace into emp (id, empno,ename,address) (select emp_temp.id,emp_temp.empno,emp_temp.ename,emp.address from emp left join emp_temp on emp.id=emp_temp.id);
0
 
LVL 4

Expert Comment

by:joep1978
ID: 24237845
Oops I meant

replace into emp (id, empno,ename,address,mystatus) (select emp_temp.id,emp_temp.empno,emp_temp.ename,emp.address,'OK' from emp left join emp_temp on emp.id=emp_temp.id);
0
 
LVL 41

Expert Comment

by:Sharath
ID: 24237981

replace into emp (id, empno,ename,address,mystatus) 
(select emp_temp.id,emp_temp.empno,emp_temp.ename,emp_temp.address,ifnull(emp.mystatus,'')
  from emp_temp left join emp on emp.id=emp_temp.id);

Open in new window

0
 
LVL 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 24238055
joep1978 - your query needs to be modifed little bit for the asker's requirement. check the attached.

mysql> select * from emp;
+----+--------+-------+----------+----------+
| id | empno  | ename | address  | mystatus |
+----+--------+-------+----------+----------+
|  1 | 123456 | PKM   | Address1 | OK       |
|  2 | 234567 | KSB   | Address2 | OK       |
|  3 | 345678 | KMM   | Address3 | OK       |
+----+--------+-------+----------+----------+
3 rows in set (0.00 sec)
 
mysql> select * from emp_temp;
+----+--------+-------+---------+
| id | empno  | ename | address |
+----+--------+-------+---------+
|  1 | 123456 | PKM   | Add1    |
|  2 | 234567 | KSB   | Add2    |
|  3 | 345678 | KMM   | Add3    |
|  4 | 456789 | MMG   | Add4    |
+----+--------+-------+---------+
4 rows in set (0.00 sec)
 
-- joep1978 solution won't update/insert your emp table.
mysql> replace into emp (id, empno,ename,address,mystatus) (select emp_temp.id,e
mp_temp.empno,emp_temp.ename,emp.address,'OK' from emp left join emp_temp on emp
.id=emp_temp.id);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
 
mysql> select * from emp;
+----+--------+-------+----------+----------+
| id | empno  | ename | address  | mystatus |
+----+--------+-------+----------+----------+
|  1 | 123456 | PKM   | Address1 | OK       |
|  2 | 234567 | KSB   | Address2 | OK       |
|  3 | 345678 | KMM   | Address3 | OK       |
+----+--------+-------+----------+----------+
3 rows in set (0.00 sec)
 
-- infact try this.
mysql> replace into emp (id, empno,ename,address,mystatus)
    -> (select emp_temp.id,emp_temp.empno,emp_temp.ename,emp_temp.address,ifnull
(emp.mystatus,'')
    ->   from emp_temp left join emp on emp.id=emp_temp.id);
Query OK, 7 rows affected (0.01 sec)
Records: 4  Duplicates: 3  Warnings: 0
 
mysql> select * from emp;
+----+--------+-------+---------+----------+
| id | empno  | ename | address | mystatus |
+----+--------+-------+---------+----------+
|  1 | 123456 | PKM   | Add1    | OK       |
|  2 | 234567 | KSB   | Add2    | OK       |
|  3 | 345678 | KMM   | Add3    | OK       |
|  4 | 456789 | MMG   | Add4    |          |
+----+--------+-------+---------+----------+
4 rows in set (0.00 sec)

Open in new window

0
 
LVL 7

Author Closing Comment

by:karunamoorthy
ID: 31574727
Thank you Sharath_123.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

873 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