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

0
437

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

Sponsored
Sponsored
Sponsored
Sponsored
Sponsored
Search
Sponsored
Virtuala FansOnly
CDN FREE
Cloud Convert
Categories
Read More
Sports
Who desires towards get Larry Lucchinos residence?
The temperature is not meant toward be a hefty variable of baseball investigate. Thus thank god...
By Perales Perales 2025-05-04 03:52:14 0 508
Other
IPO贏家未上市股票交流網|提供可靠安全的交易資訊平台
未上市股票交易首選平台!即時股價更新 × 安全交割保障。買賣未上市/下市櫃股票、員工持股,促進新創投資流動性,小額投資人最佳夥伴。 未上市股票交易平台 與...
By Shabirkhan 7sk 2025-10-03 05:51:26 0 73
Art
De ontwikkelaars zeggen dat GPT-5 een gemengd pakket is
## Inleiding In een wereld waar technologie ons dagelijks leven steeds meer vormgeeft, blijven...
By Tom Pepijn 2025-08-16 23:05:29 1 527
Other
Creatine Monohydrate: The Ultimate Supplement for Strength and Performance
If you're looking to boost your strength, endurance, and muscle recovery, Creatine Monohydrate is...
By Seoservicesin India 2025-03-02 04:14:15 0 582
Art
क्यों नहीं है ब्लैक आईफोन 17 प्रो?
आईफोन, एप्पल का एक बहुत ही प्रतिष्ठित और पसंदीदा उत्पाद है। जब भी एप्पल एक नया आईफोन लॉन्च करती...
By Rachna Bina 2025-09-14 08:05:20 1 561
Sponsored
Virtuala FansOnly https://virtuala.site