Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a mysql query that will read data from one table and write to another?

Tags:

sql

mysql

If this is possible, please provide a sample query or two so I can see how it would work. Both tables will be in the same database.

Thanks!

like image 956
Tad Donaghe Avatar asked Oct 27 '08 02:10

Tad Donaghe


2 Answers

Pseudo code:

insert into <target-table>
( <column-list> )
select <columns>
  from <source-table>
like image 116
Andrew Avatar answered Nov 10 '22 14:11

Andrew


INSERT...SELECT is the answer; see http://dev.mysql.com/doc/refman/5.1/en/insert-select.html.

For example:

INSERT INTO names
SELECT last_name, first_name
FROM people
like image 29
Rob Avatar answered Nov 10 '22 15:11

Rob