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

0
881

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

Sponsor
Sponsor
Sponsor
Sponsor
Sponsor
Căutare
Sponsor
Virtuala FansOnly
CDN FREE
Cloud Convert
Categorii
Citeste mai mult
Art
GTA 6: Il Primo AAAAA dell'Industria secondo il Co-Fondatore di Devolver
GTA 6, AAAAA, Devolver, industria videoludica, ActuGaming, co-fondatore, videogiochi, attesa,...
By Roberto Manuel 2025-09-06 02:05:24 1 2K
Food
Frozen Snacks Market Trends Driving the Shift Toward Convenient and Ready-to-Cook Foods
As people seek convenient yet high-quality food options, the frozen snack industry has emerged as...
By Amol Shinde 2026-01-20 02:35:38 0 31
Art
Microsoft Eliminó el Soporte para el Auricular WMR? Sin Problemas
auricular WMR, soporte Microsoft, Windows Mixed Reality, HP Reverb, Windows 11, realidad virtual,...
By Mateo Luis 2025-09-01 02:05:23 1 2K
Jocuri
Microsoft Accelerates File Protection with Hardware-Enhanced BitLocker
BitLocker, hardware acceleration, file protection, Microsoft, NVMe storage, data security,...
By Tao Ping 2026-01-17 11:05:21 0 248
Art
Die Vielseitigkeit des Longer Nano Laser Gravierers: Ein Blick auf eine tragbare Lösung
## Einleitung In der Welt der Lasergravur ist der Longer Nano Laser Gravierer ein Gerät, das in...
By Frida Lina 2025-08-29 03:05:15 1 2K
Sponsor
Virtuala FansOnly https://virtuala.site