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
Outro
Large Language Model Market Report, Segments, Share, Trends & Forecast 2032
Large Language Models abbreviated LLMs have become a revolutionary technology that has disrupted...
Por Biswajit Swain 2026-01-19 07:21:16 0 25
Outro
India Residential Real Estate Market, Analysis, Trends, Growth and Forecast (2024-2032)
According to the Univdatos, rapid urbanization, growing government initiatives, and low interest...
Por Praveen Gupta 2025-10-15 07:34:28 0 542
Sports
ChatGPT Ads: A New Era for Performance Budgets in Marketing
ChatGPT, Ads, Performance Budgets, eCommerce, B2B Marketing, Digital Advertising, AI Marketing,...
Por Pauline Juliette 2026-01-28 16:05:38 0 50
Outro
Quick tips to bring interiors to life: how colour, light and texture can transform your digital art
Quick tips to bring interiors to life: how colour, light and texture can transform your digital...
Por Lana Jurčić 2025-07-13 09:02:51 0 586
Patrocinado
Virtuala FansOnly https://virtuala.site