MySQL #1093 - 你不能在 FROM 子句中为更新指定目标表“赠品”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8333376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:52:37  来源:igfitidea点击:

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

mysqlsqlselectsubquerymysql-error-1093

提问by Eray

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 FROMclause

#1093 - 您不能为更新中的FROM子句指定目标表“赠品”

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

这篇文章似乎相关,但我无法根据我的查询进行调整。我怎样才能让它工作?

回答by ipr101

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)

回答by Matthew

This is because your update could be cyclical... what if updating that record causes something to happen which made the WHEREcondition 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.

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

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

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

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

Read more

阅读更多

回答by Nicola Cossu

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

回答by Tarun

create table GIVEAWAYS_NEW as(select*from giveaways);

update giveaways set winner=1
where Id=(select max(Id)from GIVEAWAYS_NEW);

回答by DARSHAN SHINDE

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;

回答by Rafael

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

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