Darius
Darius

Reputation: 1664

MYSQL: Inserting multiple results from SELECT results into a table

How do I insert the results of multiple rows of results into MYSQL?

My SQL results are :

user_id |   Name   | Age
--------+----------+-----
      1 | Rob      | 23
      2 | Jenny    | 35
      3 | Brock    | 18
      4 | Samantha | 46

How do I insert all the results into another SQL table? I'm guessing there is a "foreach row as ...." php function. I am using Zend Framework.

Upvotes: 2

Views: 2200

Answers (2)

mario
mario

Reputation: 145512

This can be done without involving PHP much. Most databases can handle that themselves:

INSERT INTO second_table (user_id, Name, Age)
   SELECT user_id, Name, Age
   FROM first_table

Here's the Mysql info on it: http://dev.mysql.com/doc/refman/5.1/de/insert-select.html

Upvotes: 3

zerkms
zerkms

Reputation: 255155

INSERT INTO tbl (user_id, name, age)
     SELECT user_id,
            name,
            age
       FROM tbl2
      WHERE ...

Upvotes: 4

Related Questions