从同一个表中选择作为插入或更新

发布于 2024-07-07 21:30:11 字数 534 浏览 7 评论 0原文

显然下面的说法是不正确的。

INSERT INTO `aTable` (`A`,`B`) VALUES((SELECT MAX(`A`) FROM `aTable`)*2),'name');

我得到的值:

SQL查询:

INSERT INTO `aTable` (`A`, `B` )
VALUES 
(
  (
   SELECT MAX(`A`)
   FROM `aTable`
  ) *2
 , 'name'
)

MySQL说:

1093 - You can't指定目标表'aTable'用于FROM子句中的更新

所以,我试图制作一个位图表,每一行对应一个位,并且有一个“地图”值。

要在表中插入,我不想执行两个查询,我想做一个。 我该怎么做?

没有人对此发表评论,但是由于我正在尝试制作位图,所以应该是 * 2 而不是 ^ 2,我的错误,请注意这就是为什么评论经常说 ^ 2,这是评论者版本中的错误读。

Clearly the following is incorrect.

INSERT INTO `aTable` (`A`,`B`) VALUES((SELECT MAX(`A`) FROM `aTable`)*2),'name');

I get the value:

SQL query:

INSERT INTO `aTable` (`A`, `B` )
VALUES 
(
  (
   SELECT MAX(`A`)
   FROM `aTable`
  ) *2
 , 'name'
)

MySQL said:

1093 - You can't specify target table 'aTable' for update in FROM clause

So, I'm trying to make a bitmap table, each row corresponds to one Bit, and has a 'map' value.

To insert in the table, I don't want to do two queries, I want to do one.
How should I do this?

No one commented on this, but since I am trying to make a bitmap, it should be * 2 not ^ 2, my mistake, please note that is why the comments often say ^ 2, it was an error in the version that the commenters read.

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

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

发布评论

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

评论(5

李不 2024-07-14 21:30:11

尝试:

insert into aTable select max(a)^2, 'name' from aTable;

或者

insert into aTable select max(a)^2, 'name' from aTable group by B;

如果您需要加入,您可以这样做:

insert into aTable select max(a)^2, 'name' from aTable, bTable;

我的“服务器版本”是“5.0.51b-community-nt MySQL Community Edition (GPL)”

try:

insert into aTable select max(a)^2, 'name' from aTable;

or

insert into aTable select max(a)^2, 'name' from aTable group by B;

If you need a join, you can do this:

insert into aTable select max(a)^2, 'name' from aTable, bTable;

My "Server version" is "5.0.51b-community-nt MySQL Community Edition (GPL)"

一刻暧昧 2024-07-14 21:30:11

实际上,您可以在插入时为表添加别名。 我到处都看到过这个问题,但似乎没有人尝试过。 使用子查询从表中获取最大值,但在子查询中为表指定别名。

INSERT INTO tableA SET fieldA = (SELECT max(x.fieldA) FROM tableA x)+1;

一个更复杂的示例,您有一个相应的辅助键,并且可能会插入相应辅助键的第一条记录:

INSERT INTO tableA SET secondaryKey = 123, fieldA = COALESCE((SELECT max(x.fieldA) FROM tableA x WHERE x.secondaryKey = 123)+1,1);

通过为表添加别名,它不会抛出错误并且似乎可以工作。 我只是在编码时这样做了,虽然我看不出上面是否有任何愚蠢的语法错误,但我会尝试这种类型的语法。

Actually, you can alias the table on the insert. I've seen this question all over the place, but no one seems to have tried that. Use a subquery to get the max from the table, but alias the table in the subquery.

INSERT INTO tableA SET fieldA = (SELECT max(x.fieldA) FROM tableA x)+1;

A more complex example, where you have a corresponding secondary key and might be inserting the FIRST record for the corresponding secondary key:

INSERT INTO tableA SET secondaryKey = 123, fieldA = COALESCE((SELECT max(x.fieldA) FROM tableA x WHERE x.secondaryKey = 123)+1,1);

By aliasing the table, it doesn't throw the error and seems to work. I just did this while coding something, although I can't see if there area any silly syntax errors above, I would try that type of syntax.

栩栩如生 2024-07-14 21:30:11

我认为 INSERT ... SELECT 不是没工作吗? 我在它的文档中看到了这一点:

INSERT的目标表
语句可能出现在FROM中
的 SELECT 部分的子句
询问。 (这在某些情况下是不可能的
旧版本的 MySQL。)在此
案例,MySQL创建临时表
保存来自 SELECT 的行和
然后将这些行插入到
目标表。

出于好奇,您使用的是哪个版本的 MySQL?

I take it that INSERT ... SELECT isn't working? I see this in the documentation for it:

The target table of the INSERT
statement may appear in the FROM
clause of the SELECT part of the
query. (This was not possible in some
older versions of MySQL.) In this
case, MySQL creates a temporary table
to hold the rows from the SELECT and
then inserts those rows into the
target table.

Out of curiosity, which version of MySQL are you using?

空心空情空意 2024-07-14 21:30:11

我认为你需要删除“VALUES”,并有一个有效的选择语句。

查看此链接

我我并不是特别喜欢 mySQL,我主要使用 MSSQL。 但是如果你正确地格式化 select 语句,它应该可以工作。

I think you need to drop the "VALUES", and have a valid select statement.

see this link

I'm not particularly a mySQL guy, I use MSSQL mostly. But If you format the select statement correctly, It should work.

再可℃爱ぅ一点好了 2024-07-14 21:30:11

一旦选择正确,您就可以执行此操作。

as soon as the Select is correct you can do this.

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