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

Προωθημένο
Προωθημένο
Προωθημένο
Αναζήτηση
Virtualbook
CDN FREE
Κατηγορίες
Διαβάζω περισσότερα
Art
NVIDIA ने SIGGRAPH 2025 में दो नए GPUs और सर्वर का खुलासा किया
NVIDIA, GPU की दुनिया में एक ऐसा नाम, जो अक्सर हमसे बेजा उम्मीदें रखता है, ने SIGGRAPH 2025 में...
από Seema Vandana 2025-08-12 18:05:24 1 95
Health
Finding a Parent Coach Near Me: Your Guide to Family Support.
In this article, we’ll explore what parent coaching is, how to find the right coach for...
από Alex Barg 2024-10-30 13:52:59 0 629
άλλο
Nakon što su ih oteli i priveli iz voda uz obalu Egipta, Greta Thunberg i aktivisti će biti deportirani iz Izraela
Nakon što su ih oteli i priveli iz voda uz obalu Egipta, Greta Thunberg i aktivisti će biti...
από Martina Lukačić 2025-06-10 07:56:08 0 216
Art
Curso intensivo en impresión 3D para órtesis y prótesis: domina todo el flujo digital
Impression 3D, ortoprotésica, curso intensivo, fabricación aditiva, diseño personalizado,...
από Katharina Sara 2025-08-31 20:05:23 1 45
άλλο
Ugledni klimatolog upozorio na toplinske valove i žestoke oluje: 'Mi za to nismo spremni'
Ugledni klimatolog upozorio na toplinske valove i žestoke oluje: 'Mi za to nismo spremni'...
από Vedrana Kašić 2025-06-26 12:56:10 0 248
Προωθημένο
Virtuala FansOnly https://virtuala.site