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

Sponsorizzato
Sponsorizzato
Sponsorizzato
Sponsorizzato
Sponsorizzato
Cerca
Sponsorizzato
Virtuala FansOnly
CDN FREE
Cloud Convert
Categorie
Leggi tutto
Art
क्यों नहीं है ब्लैक आईफोन 17 प्रो?
आईफोन, एप्पल का एक बहुत ही प्रतिष्ठित और पसंदीदा उत्पाद है। जब भी एप्पल एक नया आईफोन लॉन्च करती...
By Rachna Bina 2025-09-14 08:05:20 1 584
Art
Candle Oscillator Heißt Jetzt Alles An
Kerzen, Uhren, und eine Prise Humor, das klingt nach dem perfekten Rezept für einen interessanten...
By Marie Isabel 2025-08-23 11:05:22 1 91
Art
反向工程Aleratec CD切换器用于存档用途
## 反向工程Aleratec CD切换器:存档的可怕现实...
By Yu Yi 2025-09-14 23:05:31 1 298
Health
Creating a Reliable Operational Backbone for Therapy Clinics with Remote Role Clarity
  The Reality: Clinical Excellence Needs Administrative Support Running a successful...
By Yotohik995 Yotohik995 2025-07-07 06:02:29 0 224
Sponsorizzato
Virtuala FansOnly https://virtuala.site