【问题标题】:Insert into MySQL a huge number of rows, based on subquery... having trouble基于子查询向 MySQL 中插入大量行...遇到问题
【发布时间】:2011-08-23 00:58:32
【问题描述】:

所以,我要做的是为表中存在的每个国家/地区插入一行 NONE, $country。

应该是这样的

   Afghanistan, NONE
   Albania, NONE
   Andorra, None

... 也就是说,除了为每个国家/地区列出的省份之外……它们看起来像这样:

| 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  

这是我正在尝试的代码,但失败得很惨。

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

我刚刚得到

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

但它也抛出错误:

Subquery returns more than 1 row

【问题讨论】:

    标签: mysql mysql-error-1093 mysql-error-1242


    【解决方案1】:
    insert into countries2 (province,country) 
     select distinct 'NONE', country from countries2
    

    您可能需要检查字段的顺序!

    【讨论】:

    • 完美、独特的“无”。哈哈
    【解决方案2】:

    我猜您实际上只是想在此处更新现有表? 试试看

    UPDATE countries2 SET province = 'NONE'
    

    【讨论】:

    • 其实这个表包含上千个条目,我只是想在每个国家的省份列表中添加“NONE”。
    猜你喜欢
    • 2012-08-11
    • 2019-09-14
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    相关资源
    最近更新 更多