我想要一个更新 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
我该怎么做才能做到这一点?
如果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)
正如 AVD 所说,使用这样的表别名
UPDATE jobs
SET sectors= '12'
WHERE ID = (select * from (SELECT MAX(ID) FROM jobs) as t)