2

I want a query that updates the max number of the id... here is my code..

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

the error says : #1093 - You can't specify target table 'wpjb_job' for update in FROM clause

what can I do to make this happen?

0

2 Answers 2

3

If id is a unique field then you can use the query below:

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

If it is not unique then you need to use a temporary table:

UPDATE `jobs`
SET `sectors` = '12'
WHERE `id` = (SELECT `id` FROM (SELECT MAX(`id`) AS `id` FROM `jobs`) as temp)
Sign up to request clarification or add additional context in comments.

Comments

0

as AVD says use table alias like this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.