Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating a table from query results (mysql)

Tags:

mysql

I would like to fill a table with the results of a query on existing table. How can I do that?

like image 457
Nir Avatar asked Mar 10 '09 21:03

Nir


People also ask

How do you create a table from the result of a query in MySQL?

If the destination table does not exist, you can create it first with a CREATE TABLE statement, and then copy rows into it with INSERT ... SELECT . A second option is to use CREATE TABLE ... SELECT , which creates the destination table directly from the result of the SELECT .

How do you put query results in a table?

From the Query Designer menu, point to Change Type, and then click Insert Results. In the Choose Target Table for Insert Results Dialog Box, select the table to copy rows to (the destination table).

What are two methods that can be used to populate tables with data?

After a database has been created, there are two ways of populating the tables – either from existing data or through the use of the user applications developed for the database.

How do you populate a table in SQL?

Populate one table using another table. You can populate the data into a table through the select statement over another table; provided the other table has a set of fields, which are required to populate the first table.


2 Answers

(You don't need to match the table schemas)

INSERT tbl_name (col1, col2)
    SELECT value1, value2
    FROM othertable

See the reference for INSERT ... SELECT Syntax

like image 134
James L Avatar answered Oct 26 '22 22:10

James L


insert into table_name ...
select * from table_name where ....

The target table and the source query must match in number of columns and datatypes

See this link

like image 26
OscarRyz Avatar answered Oct 26 '22 22:10

OscarRyz