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

0
883

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

Sponsorizzato
Sponsorizzato
Sponsorizzato
Sponsorizzato
Sponsorizzato
Cerca
Sponsorizzato
Virtuala FansOnly
CDN FREE
Cloud Convert
Categorie
Leggi tutto
Food
Glucose Syrup Market Trends 2032 | Revenue, Demand & Competitive Landscape
Glucose Syrup Market Overview: During the projection period of 2022 to 2030, the global market...
By Cassie Tyler 2025-06-05 13:11:14 0 2K
Theater
Derivas_26: A Reflection on the Act of Seeing in Murcia
design conferences, Murcia, Derivas 26, Escuela Superior de Diseño, visual culture, design...
By Zélie Victoria 2026-01-19 22:05:24 0 116
Altre informazioni
MEA green hydrogen Market, Size, Share, Growth, Trends and Forecast (2024-2032)
According to the UnivDatos, various key players in the renewable sector are collaborating to...
By Praveen Gupta 2025-12-08 08:33:52 0 702
Art
Test de Girlfriend GPT: Kann man wirklich die ideale Beziehung mit dieser revolutionären KI simulieren?
Girlfriend GPT, virtuelle Beziehungen, KI-Technologie, Beziehungssimulation, digitale...
By Greta Frida 2025-09-06 07:05:18 1 2K
Altre informazioni
Choke and Kill Manifolds Market, Size, Share, Growth, Trends and Forecast (2024-2032)
According to the UnivDatos, Increasing Global energy consumption is predominantly in developing...
By Praveen Gupta 2025-12-08 05:49:36 0 980
Sponsorizzato
Virtuala FansOnly https://virtuala.site