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

0
882

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

Sponsorluk
Sponsorluk
Sponsorluk
Sponsorluk
Sponsorluk
Site içinde arama yapın
Sponsorluk
Virtuala FansOnly
CDN FREE
Cloud Convert
Kategoriler
Read More
Other
Moll FlexCut 760 Digital Rotary Die Cutter
The Moll FlexCut 760 Digital Rotary Die Cutter creates a distinct position in the Label, Mail,...
By Shabirkhan 7sk 2025-11-19 06:12:54 0 481
Other
Inside the studio of anime artists Linnea Kataja and Bellamy Luna Brooks
Inside the studio of anime artists Linnea Kataja and Bellamy Luna Brooks (Image credit:...
By Augestina Powers 2025-07-13 08:18:20 0 1K
Art
Vidalia: un horror que brota de la tierra
horror, cine estudiantil, ESMA, Vidalia, personajes de verduras, corto, diseño de personajes,...
By Miguel Felipe 2025-08-26 00:05:27 1 837
Health
GP Shield - Software-Based Duress Alerts in Real Time
GP Shield protected with real-time software-based duress alerts. Advanced software duress alarm...
By Shabirkhan 7sk 2025-03-11 04:40:29 0 1K
Crafts
Pennsylvania high school baseball team wins state title on bizarre balk-off
It was a balk-off championship victory. The Pennsylvania 6A high school baseball championship was...
By Verna Skiles 2025-10-27 03:49:39 0 1K
Sponsorluk
Virtuala FansOnly https://virtuala.site