2

我想要一个更新 id 最大数量的查询......这是我的代码..

UPDATE `jobs` SET `sectors` = '12' WHERE `id` = (SELECT MAX(`id`) FROM `jobs`)

错误说:#1093 - You can't specify target table 'wpjb_job' for update in FROM clause

我该怎么做才能做到这一点?

4

2 回答 2

3

如果id是一个唯一字段,那么您可以使用以下查询:

UPDATE `jobs` 
SET `sectors` = '12' 
ORDER BY `id` DESC LIMIT 1

如果它不是唯一的,那么您需要使用临时表:

UPDATE `jobs`
SET `sectors` = '12'
WHERE `id` = (SELECT `id` FROM (SELECT MAX(`id`) AS `id` FROM `jobs`) as temp)
于 2012-09-24T04:54:53.110 回答
0

正如 AVD 所说,使用这样的表别名

  UPDATE jobs
    SET sectors= '12'
    WHERE ID = (select * from (SELECT MAX(ID) FROM jobs) as t)
于 2012-09-24T04:56:59.273 回答