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

0
451

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

Sponzorirano
Sponzorirano
Sponzorirano
Sponzorirano
Sponzorirano
Traži
Sponzorirano
Virtuala FansOnly
CDN FREE
Cloud Convert
Kategorije
Opširnije
Gardening
Enhancing Nursing Education Through Practicum, Final Care Plans, and Professional Growth
  Nursing education is undergoing a transformative shift—combining rigorous academic...
Od 4356534 Uchiha 2025-07-23 09:42:57 0 494
Ostalo
Astrolozi tvrde:Ovih šest horoskopskih znakova najvjerojatnije će dobiti na lutriji
Astrolozi tvrde: 'Ovih šest horoskopskih znakova najvjerojatnije će dobiti na lutriji'...
Od Drago Merkaš 2025-06-13 15:14:59 0 409
Art
Ziff Davis licenzia il personale di IGN dopo mesi di acquisizioni frenetiche
Ziff Davis, IGN, licenziamenti, acquisizioni, industria dei videogiochi, notizie tecnologiche,...
Od Teresa Nicole 2025-08-05 20:05:22 1 323
Art
Criando Gaussian Splats no Blender a partir de Imagens de Drones com o Add-On Gratuito SkySplat
## Introdução Se você é um entusiasta de Blender, já deve estar farto de ferramentas que...
Od Rogério Eduardo 2025-08-17 05:05:20 1 342
Sponzorirano
Virtuala FansOnly https://virtuala.site