Workaround for MySQL’s “can’t specify target table for update in FROM clause” Error

0
568

jOOQ workaround for "can't specify target table for update in FROM clause"

In MySQL, you cannot do this:

create table t (i int primary key, j int);
insert into t values (1, 1);

update t
set j = (select max(j) from t) + 1;

The UPDATE statement will raise an error as follows:

SQL Error [1093] [HY000]: You can’t specify target table ‘t’ for update in FROM clause

People have considered this to be a bug in MySQL for ages, as most other RDBMS can do this without any issues, including MySQL clones:

  • MariaDB 10.2
  • SingleStore 6 (previously known as MemSQL)

Luckily, jOOQ can easily transform such queries for you, whenever you’re trying to UPDATE or DELETE a target table, with a predicate that depends on the target table itself. In those cases, jOOQ will just apply the following workaround:

update t
set j = (
  select *
  from (
    select max(j) from t
  ) t
) + 1;

Now, the query works without any syntactic issues. Similar workarounds are documented in the MySQL docs, but with jOOQ, you simply don’t have to think about this limitation.

Published by lukaseder

I made jOOQ

Sponsored
Sponsored
Sponsored
Sponsored
Sponsored
Search
Sponsored
Virtuala FansOnly
CDN FREE
Cloud Convert
Categories
Read More
Networking
CDN Providers
AdvancedHosting Akamai BelugaCDN Bunny CDN BytePlus CacheFly CDN77 CDNetworks CDNvideo...
By Salvador Smitham 2025-10-01 18:24:55 0 770
Other
Ovako u BiH savjetuju svoje turiste: "Ručak na Makarskoj rivijeri za 4,57 eura, 'ko kaže da je hrana skupa?"
Ovako u BiH savjetuju svoje turiste: "Ručak na Makarskoj rivijeri za 4,57 eura, 'ko kaže da je...
By Martina Lukačić 2025-06-10 07:56:07 0 388
Art
Samsung PRO Plus 1TB microSD-Speicherkarte mit Adapter jetzt 15 USD günstiger als die Black Friday-Preise
Samsung, microSD-Speicherkarte, PRO Plus, 1TB, Black Friday, Speichererweiterung, Technik,...
By Dina Luisa 2025-09-05 02:05:19 1 623
Art
Apple's großer Wurf zur Eliminierung der am häufigsten angegriffenen Schwachstellen des iPhones
## Einleitung Apple hat mal wieder die Bühne betreten, und was prangt da auf dem Bildschirm?...
By Niklas Emil 2025-09-14 03:05:26 1 777
Art
Вода из воздуха: устройство, способное преобразовать атмосферу в питьевую воду
## Введение На фоне мирового кризиса водоснабжения, который затрагивает более 2 миллиардов...
By أوليانا زويا 2025-09-03 22:05:18 1 512
Sponsored
Virtuala FansOnly https://virtuala.site