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

Commandité
Commandité
Commandité
Commandité
Commandité
Rechercher
Commandité
Virtuala FansOnly
CDN FREE
Cloud Convert
Catégories
Lire la suite
Autre
Tonometer Market Business Scenario, Size, Share, Growth, Insights, Industry Analysis, Trends and Forecasts Report 2033
The latest research report by Emergen Research, titled “Global Tonometer Market –...
Par Jim Raca 2025-05-21 12:20:16 0 2KB
Jeux
Advancing Nursing Knowledge Through Evidence-Based Practice: A Comprehensive Guide to NURS FPX 4025 Assessments
  In today’s dynamic healthcare environment, evidence-based practice (EBP) is the...
Par 4356534 Uchiha 2025-07-23 09:34:12 0 1KB
Autre
Global Facial Care Industry Report & Key Trends
Polaris Market Research has introduced the latest market research report titled Facial Care...
Par MAYUR YADAV 2025-12-02 13:34:23 0 560
Autre
Fakturaköp – en effektiv lösning på likviditetsutmaningar - CapIQ Finans
Fakturaköp – en effektiv lösning på likviditetsutmaningar. CapIQ....
Par Shabirkhan 7sk 2024-12-03 05:04:57 0 3KB
Diffusion en direct
Live streaming
 
Par Drago Merkaš 2025-01-05 21:35:48 0 1KB
Commandité
Virtuala FansOnly https://virtuala.site