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

0
451

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
Life
Kako je Ritz-Carlton redefinirao svijet cruisinga
Kako je Ritz-Carlton redefinirao svijet cruisingaEvrima nije samo luksuzna jahta – ona je vizija...
Por Lana Jurčić 2025-07-21 12:40:52 0 286
Food
Alginates Market Trends, Size, Share, and Forecast: Growth Opportunities and Projections (2024–2032)
Alginates Market Overview: According to MRFR analysis, the global Alginates Market is expected...
Por Cassie Tyler 2024-11-27 05:58:50 0 470
Art
# Brilliant Labs推出新智能眼镜,配备全新显示技术
智能眼镜, 显示技术, Brilliant Labs, 开放平台, 创新科技, 可穿戴设备, 黑客友好, 近眼显示, 智能设备...
Por Nan Liu 2025-08-05 00:05:23 1 228
Art
Krafton, Unknown Worlds Kurucularını Su Altında Bıraktı: Subnautica 2'yi Tercih Etmediler!
## Giriş Oyun dünyasında "Subnautica" gibi bir başyapıtın varlığı, oyunculuk deneyimini bir...
Por Mehmet Zafer 2025-08-13 23:05:37 1 364
Art
### As Buscas de Celulares na Fronteira dos EUA Atingem um Máximo Histórico
buscas de celulares, fronteira dos EUA, direitos digitais, privacidade, segurança, agentes da...
Por Laura Isabella 2025-08-22 07:05:24 1 193
Patrocinado
Virtuala FansOnly https://virtuala.site