Search code examples
mysqlmysql-error-1093mysql-error-1242

Insert into MySQL a huge number of rows, based on subquery... having trouble


So, what I am trying to do is insert a row of NONE, $country for every country that exists in the table.

It should look like

   Afghanistan, NONE
   Albania, NONE
   Andorra, None

... That is, in addition to the provinces listed for each country... they look like this:

| Zambia                    | Western                                            |
| Zimbabwe                  | Bulawayo                                           |
| Zimbabwe                  | Harare                                             |
| Zimbabwe                  | Manicaland                                         |
| Zimbabwe                  | Mashonaland Central                                |
| Zimbabwe                  | Mashonaland East                                   |
| Zimbabwe                  | Mashonaland West                                   |
| Zimbabwe                  | Masvingo                                           |
| Zimbabwe                  | Matabeleland North                                 |
| Zimbabwe                  | Matabeleland South                                 |
| Zimbabwe                  | Midlands  

This is the code I am attempting, but failing miserably.

insert into countries2 (province,country) 
VALUES ('NONE', (select distinct country from countries2));

I just get

You can't specify target table 'countries2' for update in FROM clause

But it is also throwing the error:

Subquery returns more than 1 row

Solution

  • insert into countries2 (province,country) 
     select distinct 'NONE', country from countries2
    

    you may want to check the order of the fields !