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

Patrocinados
Patrocinados
Patrocinados
Patrocinados
Patrocinados
Buscar
Patrocinados
Virtuala FansOnly
CDN FREE
Cloud Convert
Categorías
Read More
Home
48h Sanierung Bonn - Asbestkleber & Bodenbelag-0221-96986861
Von Floorflex, Cushion-Vinyl, asbesthaltigem Vinylboden bis zum Abschliff von asbesthaltigen...
By Shabirkhan 7sk 2025-04-23 05:59:24 0 399
Art
Honest Review der neuen Tiami Matratze (2025)
## Einführung Die neue Tiami Matratze (2025) hat die Matratzenlandschaft betreten und...
By Lara Frida 2025-08-29 21:05:17 1 511
Other
Global UTM Remote ID Module Market to Witness Substantial Growth Driven by UAV Regulations and Airspace Integration
The UTM Remote ID Module Market is projected to experience significant expansion in the coming...
By Riya Sharma 2025-10-13 15:21:35 0 133
Art
USDA Libera Financiamento de Agricultura para Escolas Após Cancelações Anteriores
financiamento agricultura, USDA, programa escola, agricultura para escolas, investimento na...
By Gabriel Rogério 2025-09-12 22:05:52 1 438
Art
**Efeitos de Guitarra no PlayStation Portable: O Que Você Não Sabe e Provavelmente Não Precisa Saber**
efeitos de guitarra, PlayStation Portable, distorção, fidelidade de áudio, música, hardware de...
By Diego Bruno 2025-08-13 14:05:29 1 297
Patrocinados
Virtuala FansOnly https://virtuala.site