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
Art
Génial! Der Metavers von Meta erwacht mit sprechenden NPCs zum Leben!
## Einführung In einer Welt, die zunehmend von der digitalen Realität geprägt ist, scheint der...
Por Emilia Ronja 2025-09-01 06:05:23 1 1K
Food
Tofu Market Overview, Trends & Share Analysis 2032
Tofu Market Size was valued at USD 1.5 billion in 2022. The Tofu market industry is projected to...
Por Cassie Tyler 2025-03-11 10:36:17 0 716
Art
GPS تشويش ضد طائرة رئيسة المفوضية الأوروبية: هل من روسيا؟
GPS, تشويش, طائرة, رئيسة المفوضية الأوروبية, روسيا, الهجمات الإلكترونية, بلغاريا, أورسولا فون دير...
Por هشام أنس 2025-09-02 08:05:23 1 2K
Crafts
Why Yankees think Aaron Judge is on verge of busting out
KANSAS CITY, Mo. Aaron Judge had nothing to show for it, but made three loud outs Wednesday night...
Por Verna Skiles 2025-10-27 03:48:06 0 1K
Início
Asbestsanierung in Duisburg 0231-98194868
Von A wie Asbesterkennung bis S wie Schadstoffanalyse. Wir helfen fachmännisch nach der TRGS...
Por Shabirkhan 7sk 2025-03-25 07:31:28 0 709
Patrocinado
Virtuala FansOnly https://virtuala.site