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

0
306

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

Sponsored
Sponsored
Sponsored
Search
Virtualbook
CDN FREE
Categories
Read More
Other
Luxury Niche Perfume Market Size, Share & Growth Analysis - Forecast 2032
Luxury Niche Perfume Market Overview:  Luxury niche perfumes are crafted with a focus on...
By Cassie Tyler 2024-11-27 12:29:13 0 478
Art
2025 One Hertz Challenge: Квадратные волны так, как вы хотите их видеть
2025, вызов One Hertz, генератор сигналов, функции генератора, квадратные волны, электроника,...
By Антон Максим 2025-08-06 15:30:28 1 33
Art
Die sechzehnjährige Odyssee zur Emulation der Pionierkonsole LaserActive
## Einleitung Wie oft haben wir uns über die dreisten Marketingstrategien der Gaming-Industrie...
By Carla Leni 2025-09-08 20:05:35 1 33
Art
Samsung's Muziekframe: Een Trieste Samensmelting van Geluid en Herinneringen
## Inleiding In een wereld die steeds sneller verandert, zijn er momenten waarop we even willen...
By Tom Pepijn 2025-08-24 17:05:21 1 56
Health
Arthritis Specialist in Delhi: Your Guide to Expert Care and Long-Term Relief
Arthritis is one of the most common joint-related conditions that affects millions of people...
By Dr. Gaurav Seth 2025-09-10 07:27:03 0 32
Sponsored
Virtuala FansOnly https://virtuala.site