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

Commandité
Commandité
Commandité
Commandité
Commandité
Rechercher
Commandité
Virtuala FansOnly
CDN FREE
Cloud Convert
Catégories
Lire la suite
Autre
Autonomous Beyond Visual Line of Sight (BVLOS) Drone Market, Growth, Size, Share, Trends and forecast (2025-2033)
According to a new report by UnivDatos, the Autonomous Beyond Visual Line of Sight (BVLOS) Drone...
Par Praveen Gupta 2025-10-21 09:46:50 0 169
Art
**Outright Games’in Yönetimi: Gerçekten Para Kaybetmiyorlar, Daha Az Kazanıyorlar!**
## Giriş Oyun dünyasında skandallar asla bitmez! En son Outright Games’ten gelen haberler, oyun...
Par Matko Tuna 2025-08-03 17:05:39 1 299
Art
RFK Jr. Behält Landwirtschaftsinteressen in der MAHA-Agenda
RFK Jr., Landwirtschaft, MAHA-Agenda, Gesundheitspolitik, Kongress, Senatoren,...
Par Leonie Johanna 2025-09-04 22:05:16 1 306
Art
fxpodcast: Detaillierte Analyse der VFX-Sequenz des Angriffs auf Air Force One in Heads of State
## Die schmerzliche Realität der visuellen Effekte in Heads of State In einer Welt, in der die...
Par Melina Mathilda 2025-09-09 15:05:18 1 535
Jeux
Understanding Black Ops 7 DLC Content and Free Updates with Bo7 Bot Lobby Service
When investing $70 in Black Ops 7, players naturally want to know about the DLC content and the...
Par Claus Oliver 2025-10-20 06:05:45 0 301
Commandité
Virtuala FansOnly https://virtuala.site