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

0
309

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

Gesponsert
Gesponsert
Gesponsert
Suche
Virtualbook
CDN FREE
Kategorien
Mehr lesen
Art
Borderlands 4: Gearbox ने गेम के लॉन्च का समय और डे-वन पैच की जानकारी दी है
Borderlands 4, Borderlands श्रृंखला का अगला भाग, अब हमारे दरवाजे पर दस्तक दे रहा है। Gearbox ने...
Von Seema Vandana 2025-09-09 20:05:22 1 43
Andere
PMR or PMR446 Frequency List
The Private Mobile Radio (PMR) service is commonly used in Europe and other regions for...
Von Drago Merkaš 2025-01-07 17:48:53 0 480
Startseite
48h Sanierung Bonn - Asbestkleber & Bodenbelag-0221-96986861
Floorflexplatten Bonn - Von Floorflex, Cushion-Vinyl, asbesthaltigem Vinylboden bis zum...
Von Shabirkhan 7sk 2024-11-11 10:37:49 0 449
Gardening
Why You Should Join the American Rose Society
Why You Should Join the American Rose Society | La Vie en Roses | Episode 6...
Von Martina Lukačić 2025-05-31 19:55:04 0 273
Art
La saison 2 de Tomb Raider: The Legend of Lara Croft - Ein Abschied mit Stil
Tomb Raider, Lara Croft, letzte Staffel, Animationsserie, Dezember 2023, Abschied, Gaming ## Ein...
Von Marie Isabel 2025-09-05 00:05:26 1 35
Gesponsert
Virtuala FansOnly https://virtuala.site