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

0
883

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

Sponsor
Sponsor
Sponsor
Sponsor
Sponsor
Zoeken
Sponsor
Virtuala FansOnly
CDN FREE
Cloud Convert
Categorieën
Read More
Other
AI in Virtual Labs Market annual growth rate (CAGR) of 24.7% from 2025 to 2033
The AI in Virtual Labs Market is rapidly evolving, gaining traction across academic, research,...
By Sadaf Sheikh 2025-10-10 06:04:52 0 561
Spellen
Fc 26 Coins Are Here To Help You Out
EA Sports' FUT Ultimate Team currency allows players to unlock rare player cards quickly, speed...
By Dawcear Cear 2025-11-05 05:40:43 0 1K
Art
Congressional Budget Office Berichtet, dass Mehr als 2,7 Millionen Amerikaner SNAP-Leistungen Verlieren Werden
SNAP, Lebensmittelhilfe, Budget, US-Politik, CBO-Bericht, Familienhilfe, Lebensmittelpreise,...
By Mathilda Leonie 2025-08-12 23:05:16 1 620
Other
Why Pre Purchase Vehicle Inspection in Sydney is Essential Before Buying a Used Car
Buying a used car can be a great way to save money, but it also comes with its own set of risks....
By Prepurchasecarinspector Official 2025-05-11 14:38:08 0 2K
Other
Autonomous Navigation Market Report, Segments, Share, Trends & Forecast 2032
According to the Univdatos analysis, growing investments in Autonomous Navigation for improving...
By Biswajit Swain 2026-01-19 08:36:41 0 26
Sponsor
Virtuala FansOnly https://virtuala.site