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

Προωθημένο
Προωθημένο
Προωθημένο
Προωθημένο
Προωθημένο
Αναζήτηση
Προωθημένο
Virtuala FansOnly
CDN FREE
Cloud Convert
Κατηγορίες
Διαβάζω περισσότερα
Art
GTA 6: Il Primo AAAAA dell'Industria secondo il Co-Fondatore di Devolver
GTA 6, AAAAA, Devolver, industria videoludica, ActuGaming, co-fondatore, videogiochi, attesa,...
από Roberto Manuel 2025-09-06 02:05:24 1 2χλμ.
άλλο
Top 10 Market Research Companies in 2025: UnivDatos Ranked Among Leaders
The year 2025 marks a significant transformation in the global market intelligence landscape. As...
από Biswajit Swain 2025-11-20 13:04:37 0 821
άλλο
Best CBD Digital Marketing Agencies in the United States for Serious Results
In today’s fast-evolving digital world, CBD brands are booming. But with growth comes...
από The Millenials 2025-10-15 11:44:00 0 2χλμ.
Art
**Bambu Lab presenta la sua nuova stampante 3D, la H2S: una speranza che svanisce**
stampante 3D, Bambu Lab, H2S, tecnologia, innovazione, stampa 3D, design, creatività, futuro...
από Elena Irene 2025-08-26 15:05:23 1 841
Προωθημένο
Virtuala FansOnly https://virtuala.site