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

Patrocinados
Patrocinados
Patrocinados
Patrocinados
Patrocinados
Buscar
Patrocinados
Virtuala FansOnly
CDN FREE
Cloud Convert
Categorías
Read More
Fitness
Stellar Blade Dev Believes ‘One Person Can Perform The Work Of 100 People’ With AI
AI, Shift Up, Stellar Blade, game development, productivity, gaming industry, competition,...
By Sergio Paulo 2026-01-14 19:05:22 0 177
Other
股票下市怎麼辦?下市股票的處理建議
當一家公司的股票下市,並不代表投資人手中的股票會變成無價值的紙張。實際上,這間公司還是存在,只是沒有上市,它們只能在場外交易市場私人間買賣,這可能會導致股票交易量少、流動性較差。...
By Shabirkhan 7sk 2025-09-17 06:37:09 0 571
Art
بناء علاقات أقوى مع العملاء: 6 طرق فعّالة
## مقدمة في عالم الأعمال، هناك قاعدة ذهبية لا يمكن تجاهلها: "العميل هو الملك، ولكن الملك يحتاج...
By هشام أنس 2025-08-04 17:05:26 1 670
Gardening
# Personal Branding: What It Is and How to Create Yours? Includes Examples
personal branding, personal brand, personal branding tips, build your brand, brand identity,...
By Onur Sinan 2026-01-20 15:05:32 0 118
Art
Matter and Form Three 3D Scanner Review: Easy Scans
## परिचय आजकल की दुनिया में, 3D स्कैनिंग तकनीक ने नई ऊचाइयों को छू लिया है। कई लोग, चाहे वे...
By Anil Ajay 2025-08-11 02:05:24 1 693
Patrocinados
Virtuala FansOnly https://virtuala.site