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

Commandité
Commandité
Commandité
Rechercher
Virtualbook
CDN FREE
Catégories
Lire la suite
Art
Le studio Wolf Smoke célèbre 20 ans d’animation
## Ein Blick auf 20 Jahre Wolf Smoke Es gibt Momente in der Geschichte der Animation, die nicht...
Par Jana Frida 2025-09-01 12:05:26 1 42
Art
Interstellar Besuch oder bloßer Hype? Die Wahrheit über 3I/Atlas
Interstellar, Astronomie, 3I/Atlas, Avi Loeb, Weltraum, Wissenschaft, Hype, Astronomie...
Par Nele Marie 2025-08-17 21:05:29 1 32
Art
Bose QuietComfort Ultra Earbuds (2. Generation): Exzellente Ohrhörer
Bose, Ohrhörer, Geräuschunterdrückung, kabelloses Laden, Audio, Technologie, Klangqualität,...
Par Lia Frieda 2025-09-08 17:05:24 1 40
Art
Die Kriterien für die Auswahl virtueller Spielräume: Ein satirischer Leitfaden
## Einleitung In einer Welt, in der das echte Leben so langweilig wie ein schlechter Film ohne...
Par Luisa Maja 2025-09-12 23:05:18 1 35
Coding
When SQL Meets Lambda Expressions
A few modern SQL dialects have started introducing lambda expressions ARRAY types are a part...
Par Luka Matijević 2025-05-31 20:25:30 0 253
Commandité
Virtuala FansOnly https://virtuala.site