基于子查询向 MySQL 插入大量行......遇到麻烦

发布于 2024-11-06 05:11:13 字数 1424 浏览 4 评论 0原文

因此,我想做的是为表中存在的每个国家/地区插入一行 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'tspecify target table 'countries2' for update in FROM Clause

但它也引发了错误:

Subquery returns more than 1 row

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

真心难拥有 2024-11-13 05:11:13
insert into countries2 (province,country) 
 select distinct 'NONE', country from countries2

您可能想检查字段的顺序!

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

you may want to check the order of the fields !

疧_╮線 2024-11-13 05:11:13

我猜您实际上只想更新此处的现有表?
尝试

UPDATE countries2 SET province = 'NONE'

I'm guessing that you actually just want to update the existing table here?
Try

UPDATE countries2 SET province = 'NONE'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文