Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Creating a new table with information from a query

In MySQL, I would like to create a new table with all the information in this query:

select * into consultaa2 from SELECT  CONCAT(    'UPDATE customers SET  customers_default_address_id= ',      (SELECT a.address_book_id FROM  address_book a where  c.customers_id=a.customers_id order by  address_book_id desc limit 1),    '  WHERE customers_id = ', customers_id,  ';') AS sql_statement FROM customers c  where c.customers_id > 3894; 

The query is too long for the browser to show the concat and I need this to make this updates.

like image 303
Saikios Avatar asked Jul 06 '11 10:07

Saikios


People also ask

How can you create a new table with existing data from another table?

A copy of an existing table can be created using a combination of the CREATE TABLE statement and the SELECT statement. The new table has the same column definitions. All columns or specific columns can be selected.

Can I create a table from a query?

On the Design tab, in the Query Type group, click Make Table. The Make Table dialog box appears. In the Table Name box, enter a name for the new table. Click the down-arrow and select an existing table name.

How do you create a new SQL table from existing SQL table?

Question: How can I create a SQL table from another table without copying any values from the old table? Answer: To do this, the SQL CREATE TABLE syntax is: CREATE TABLE new_table AS (SELECT * FROM old_table WHERE 1=2);


1 Answers

You can do it like this:

CREATE TABLE tablename SELECT * FROM othertable; 

tablename is the name of the new table you want to create, SELECT * FROM othertable is the query that returns the data the table should be created from.

like image 96
Daniel Hilgarth Avatar answered Sep 28 '22 06:09

Daniel Hilgarth