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

Patrocinado
Patrocinado
Patrocinado
Patrocinado
Patrocinado
Pesquisar
Patrocinado
Virtuala FansOnly
CDN FREE
Cloud Convert
Categorias
Leia mais
Art
3D प्रिंटिंग एक विशाल बेब्लेड एरिना
बेब्लेड, बेबलेड टॉप्स, 3D प्रिंटिंग, खिलौने, खेल, एरिना, कस्टम बेब्लेड, बेब्लेड लड़ाई, बेब्लेड...
Por Rachna Bina 2025-08-09 08:05:21 1 444
Shopping
Le gacha urbain Neverness to Everness aura bientôt droit à une bêta, et en français
## The Anticipated Beta of Neverness to Everness: A New Urban Gacha Experience In recent years,...
Por Carlos Samuel 2026-01-16 21:05:23 0 219
Outro
Agricultural Micronutrients Market Report, Segments, Share, Trends & Forecast 2032
According to the Univdatos analysis increasing awareness of plant nutrition and a growing focus...
Por Biswajit Swain 2026-01-14 11:25:40 0 47
Outro
Key Players and Growth Forecast in the Global Stem Cell Therapy Market
Introduction The Global Stem Cell Therapy Market is experiencing rapid growth, driven...
Por Credible Vicky 2025-05-20 04:35:08 0 796
Outro
全聯實業股價|未上市股票即時行情與安全交易平台 - IPO贏家
全聯實業未上市股票即時報價查詢!最新行情更新、安全交割保障、完整公司資料與投資討論區。專業平台嚴格把關,降低未上市股票交易風險,點擊查看詳情!...
Por Shabirkhan 7sk 2025-09-25 05:48:47 0 528
Patrocinado
Virtuala FansOnly https://virtuala.site