Pages

Sunday, 25 August 2013

INSERT … SELECT

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
       [INTO] tbl_name [(col_name,...)]
       SELECT ...
       [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
With INSERT ... SELECT, you can insert many rows into a table from one or many tables. This is a method to insert data very fast.

Example:
INSERT INTO employee (dept_id,emp_name)
       SELECT d.d_id as dept_id, p.name as emp_name
       FROM department as d 
       JOIN people as p 
       ON d.category_id = p.category_id
       WHERE p.category_id < 20;

In the above example we have fetched the department ids and the names from the select query and inserted into the employee table.

No comments:

Post a Comment