更新sql查询,其中select max

时间:2012-09-24 04:49:24

标签: mysql sql

我想要一个更新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

我该怎么做才能实现这一目标?

2 个答案:

答案 0 :(得分:2)

如果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)

答案 1 :(得分:0)

因为AVD说使用像这样的表别名

  UPDATE jobs
    SET sectors= '12'
    WHERE ID = (select * from (SELECT MAX(ID) FROM jobs) as t)