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

0
883

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

Sponsor
Sponsor
Sponsor
Sponsor
Sponsor
Căutare
Sponsor
Virtuala FansOnly
CDN FREE
Cloud Convert
Categorii
Citeste mai mult
Alte
南山人壽(5874)股價行情|怎麼買賣・安全交割 - IPO贏家
查詢南山人壽最新未上市股價與歷史成交區間,整理股票怎麼買賣、過戶文件與風險提醒;提供專人協助交易與安全交割建議,想買或想賣可直接留言詢價。 南山人壽股價 與...
By Shabirkhan 7sk 2026-01-02 05:53:50 0 344
Art
Wie man sein neues Android-Handy einrichtet (2025)
## Einleitung Willkommen in der Zukunft, wo dein neues Android-Handy darauf wartet, von dir...
By Katharina Leonie 2025-08-31 15:05:28 1 801
Food
Vitamin & Mineral Supplements Market Report By Category & Competition by 2032
Vitamin & Mineral Supplements Market Outlook The vitamin and mineral supplements market size...
By Cassie Tyler 2025-01-29 06:14:02 0 748
Food
What Role Does Cardamom Oil Play in the Expanding Essential Oils Industry?
Cardamom Oil Market Overview: The global cardamom oil market is poised to touch a value of USD...
By Cassie Tyler 2025-02-27 05:51:42 0 857
Sponsor
Virtuala FansOnly https://virtuala.site