MySQL #1093 - 您无法指定目标表“赠品”;用于 FROM 子句中的更新

发布于 2024-12-18 15:29:26 字数 384 浏览 1 评论 0原文

我尝试过:

UPDATE giveaways SET winner = '1' WHERE ID = (SELECT MAX(ID) FROM giveaways)

但它给出了:

#1093 - 您无法在 FROM 子句中指定要更新的目标表“赠品”

这篇文章 似乎相关,但我无法使其适应我的查询。我怎样才能让它发挥作用?

I tried:

UPDATE giveaways SET winner = '1' WHERE ID = (SELECT MAX(ID) FROM giveaways)

But it gives:

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

This article seems relevant but I can't adapt it to my query. How can I get it to work?

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

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

发布评论

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

评论(6

青巷忧颜 2024-12-25 15:29:26

根据您链接到的文章中的信息,这应该有效:

update giveaways set winner='1'
where Id = (select Id from (select max(Id) as id from giveaways) as t)

Based on the information in the article you linked to this should work:

update giveaways set winner='1'
where Id = (select Id from (select max(Id) as id from giveaways) as t)
云仙小弟 2024-12-25 15:29:26

这是因为您的更新可能是周期性的...如果更新该记录导致发生导致 WHERE 条件 FALSE 的情况怎么办?您知道情况并非如此,但引擎却不然。操作中表上也可能存在相反的锁。

我认为你可以这样做(未经测试):

UPDATE
    giveaways
SET
    winner = '1'
ORDER BY
    id DESC
LIMIT 1

了解更多

This is because your update could be cyclical... what if updating that record causes something to happen which made the WHERE condition FALSE? You know that isn't the case, but the engine doesn't. There also could be opposing locks on the table in the operation.

I would think you could do it like this (untested):

UPDATE
    giveaways
SET
    winner = '1'
ORDER BY
    id DESC
LIMIT 1

Read more

哥,最终变帅啦 2024-12-25 15:29:26
update giveaways set winner=1 
where Id = (select*from (select max(Id)from giveaways)as t)
update giveaways set winner=1 
where Id = (select*from (select max(Id)from giveaways)as t)
花想c 2024-12-25 15:29:26
create table GIVEAWAYS_NEW as(select*from giveaways);

update giveaways set winner=1
where Id=(select max(Id)from GIVEAWAYS_NEW);
create table GIVEAWAYS_NEW as(select*from giveaways);

update giveaways set winner=1
where Id=(select max(Id)from GIVEAWAYS_NEW);
極樂鬼 2024-12-25 15:29:26

使用 TEMP TABLE:

如下:

UPDATE TABLE_NAME SET TABLE_NAME.IsActive=TRUE
WHERE TABLE_NAME.Id IN (
    SELECT Id
    FROM TEMPDATA
);

CREATE TEMPORARY TABLE TEMPDATA
SELECT MAX(TABLE_NAME.Id) as Id
FROM TABLE_NAME
GROUP BY TABLE_NAME.IncidentId;

SELECT * FROM TEMPDATA;

DROP TABLE TEMPDATA;

Make use of TEMP TABLE:

as follows:

UPDATE TABLE_NAME SET TABLE_NAME.IsActive=TRUE
WHERE TABLE_NAME.Id IN (
    SELECT Id
    FROM TEMPDATA
);

CREATE TEMPORARY TABLE TEMPDATA
SELECT MAX(TABLE_NAME.Id) as Id
FROM TABLE_NAME
GROUP BY TABLE_NAME.IncidentId;

SELECT * FROM TEMPDATA;

DROP TABLE TEMPDATA;
故事与诗 2024-12-25 15:29:26

您可以先创建子查询的视图,然后更新/删除从视图中选择。
只需记住之后删除视图即可。

You can create a view of the subquery first and update/delete selecting from the view instead..
Just remember to drop the view after.

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