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

Спонсоры
Спонсоры
Спонсоры
Спонсоры
Спонсоры
Поиск
Спонсоры
Virtuala FansOnly
CDN FREE
Cloud Convert
Категории
Больше
Shopping
20 Best Online Master’s Programs in Digital Marketing in Spain
digital marketing, online master’s programs, Spain, marketing specialization, freelance...
От Sophie Yasmine 2026-01-18 22:05:39 0 98
Life
Koji je smisao života kada i onako svi umiremo?
Pitanje o smislu života je jedno od najdubljih i najtrajnijih pitanja koje ljudi postavljaju....
От Drago Merkaš 2025-01-29 21:20:24 0 2Кб
Другое
Global Pharmaceutical Cleaning Validation Market Analysis
Market OverviewAccording To The Research Report Published By Polaris Market Research, The Global...
От MAYUR YADAV 2025-09-25 09:58:44 0 410
Другое
Wildlife Tourism Market by Destination Type
The most recent research study by Polaris Market Research titled According to the research...
От MAYUR YADAV 2025-12-17 10:09:36 0 962
Networking
What is an A/B Test? Understanding the Power of Data-Driven Marketing
A/B testing, marketing strategies, conversion rate optimization, data analysis, digital marketing...
От Guo Hong 2026-01-25 19:05:13 0 69
Спонсоры
Virtuala FansOnly https://virtuala.site