Condition of a query with an under-consumption

Asked

Viewed 72 times

3

I need to update the last 110 table records relevo, but my problem lies in the current WHERE.

I’m doing it this way:

UPDATE relevo
SET id_projeto = 157
WHERE id_relevo = (SELECT id_relevo FROM relevo 
ORDER BY id_relevo DESC
LIMIT 110)

The following error is being returned:

You can’t specify target table 'relevo' for update in FROM clause

I tried it this way too:

UPDATE relevo
SET id_projeto = ?
WHERE id_relevo IN (SELECT id_relevo FROM relevo 
ORDER BY id_relevo DESC
LIMIT 110)

The error returned:

This version of Mysql doesn’t yet support 'LIMIT & IN/ALL/ANY/SOME subquery

1 answer

3


This business of updating the latest records does not work very well, is fragile, can give the expected result once and not another.

But if you’re going to insist on it do it simply, don’t use SELECT:

UPDATE relevo
    SET id_projeto = 157
    ORDER BY id_relevo DESC
    LIMIT 110

I put in the Github for future reference.

Documentation.

  • 1

    Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.