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

Sponzorováno
Sponzorováno
Sponzorováno
Sponzorováno
Sponzorováno
Hledat
Sponzorováno
Virtuala FansOnly
CDN FREE
Cloud Convert
Kategorie
Číst více
Domů
48h Sanierung Leverkusen - Asbesthaltiger Boden -0221-96986861
Von Floorflex, Cushion-Vinyl, asbesthaltigem Vinylboden bis zum Abschliff von asbesthaltigen...
Od Shabirkhan 7sk 2025-07-22 05:39:16 0 162
Networking
Global Healthcare Cognitive Computing Market Share & Size, Growth, Industry Trends | Emergen Research
The latest research report by Emergen Research, named ‘Global Healthcare Cognitive...
Od Jim Raca 2025-05-16 05:48:36 0 382
Art
2025 Bir Hertz Mücadelesi: Hayatın Oyunu
2025, bir hertz mücadelesi, proje geliştirme, zaman tutma, teknoloji, yaratıcılık, yenilikçi...
Od Onur Sinan 2025-08-13 21:05:16 1 182
Art
Was ist ein Passkey? So richten Sie sie ein und verwenden sie (2025)
## Einführung In einer Welt, in der digitale Sicherheit immer wichtiger wird, sind Passkeys ein...
Od Laura Jana 2025-09-03 21:05:34 1 349
Sponzorováno
Virtuala FansOnly https://virtuala.site