2

I would like to insert a bunch of values from old_table with multiple columns into a new_table with 1 column.

This is how I plan to make a selection:

SELECT DISTINCT City FROM old_table;

I would like to insert all the values from this selection into a column.

Normally I would use INSERT INTO table_name VALUES (value1), but this is for inserting 1 row. Now I have 10000 rows I want to insert at once. My apologies if this is already answered. I had little success with googling, due to not knowing how to formulate question.

potato
  • 4,479
  • 7
  • 42
  • 99
  • http://stackoverflow.com/questions/5907206/mysql-insert-into-tbl-select-from-another-table-and-some-default-values?rq=1 – techie_28 Mar 22 '16 at 09:01
  • http://stackoverflow.com/questions/74162/how-to-do-insert-into-a-table-records-extracted-from-another-table – techie_28 Mar 22 '16 at 09:01

3 Answers3

1

When inserting the results of a query, you don't use the VALUES keyword.

Try this:

INSERT INTO table_name
SELECT DISTINCT City
FROM old_tabl
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You can try insert into.... select statement

like

INSERT INTO table_name (col_name)
SELECT DISTINCT City
FROM old_table;

if you have number of column is same as insert table, you should try blow query

INSERT INTO table_name
SELECT DISTINCT City
FROM old_table;
Vipin Jain
  • 3,686
  • 16
  • 35
-1
    INSERT INTO def (catid, title, page, publish) 
SELECT catid, title, 'page','yes' from `abc`
Priyanshu
  • 885
  • 6
  • 12