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

Patrocinado
Patrocinado
Patrocinado
Patrocinado
Patrocinado
Pesquisar
Patrocinado
Virtuala FansOnly
CDN FREE
Cloud Convert
Categorias
Leia mais
Art
Microsoft, Xbox Oyun Stüdyoları için Rare Veteranını Atadı
## Gözyaşları Arasında Bir Geçiş Oyun dünyası, bazen mutlulukla dolu bir cennet, bazen ise...
Por Can Baran 2025-09-02 09:05:29 1 396
Art
NVIDIA, Borderlands 4 ve RTX 50 Serisi ile Geliyor: Oyun Dünyasında Bir Devrim mi?
NVIDIA, oyun dünyasında her zaman bir adım önde kalmayı başaran bir marka olarak, son bombasıyla...
Por Seda Gizem 2025-08-22 06:05:16 1 231
Food
Soups Market Trends and Performance: Size, Share, and Analytical Report
Soups Market Overview: For a very long time, soups have been hailed as the ideal comfort food....
Por Cassie Tyler 2024-10-24 06:26:51 0 570
Art
Roblox wird von Louisiana verklagt: Ein Nährboden für Sexualstraftäter
Roblox, Klage, Louisiana, Sexualstraftäter, Liz Murrill, Online-Sicherheit, Kinder, Datenschutz,...
Por Sophia Frieda 2025-08-16 15:05:35 1 291
Início
Floorflexplatten Düsseldorf — Asbestwächter-express.de
Von Floorflex, Cushion-Vinyl, asbesthaltigem Vinylboden bis zum Abschliff von asbesthaltigen...
Por Shabirkhan 7sk 2025-08-04 09:44:29 0 487
Patrocinado
Virtuala FansOnly https://virtuala.site