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

0
309

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
Pesquisar
Virtualbook
CDN FREE
Categorias
Leia mais
Art
# Самый большой детектор нейтрино собирает данные в Китае
нейтрино, детектор, физика частиц, темная материя, научные исследования, Китай, космические лучи,...
Por مارينا زلاتا 2025-09-01 16:05:18 1 21
Outro
Which fictional video game company had the most evil logo design?
Which fictional video game company had the most evil logo design? We've looked at the best...
Por Augestina Powers 2025-07-13 08:18:21 0 55
Art
NASA Sucht Freiwillige zur Verfolgung der Artemis II Mission
NASA, Freiwillige, Artemis Programm, Raumfahrt, Artemis II, Weltraummission, Raumfahrzeuge,...
Por Ronja Alina 2025-09-01 04:05:14 1 55
Art
Les agriculteurs de couleur offrent un bien-être communautaire dans les 'Fermes de guérison'
## Introduction Dans un monde où la douleur et la lutte semblent omniprésentes, un rayon...
Por Chloé Anaïs 2025-09-03 20:05:29 1 44
Outro
Discover Devotion Every Day with Govinda 365
In today’s fast-paced world, spiritual balance is essential for peace of mind. Govinda 365...
Por Govinda 365club 2025-06-04 05:01:50 0 298
Patrocinado
Virtuala FansOnly https://virtuala.site