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

Sponsor
Sponsor
Sponsor
Sponsor
Sponsor
Căutare
Sponsor
Virtuala FansOnly
CDN FREE
Cloud Convert
Categorii
Citeste mai mult
Sports
Dragon Ball Z: Kakarot Surpasses 10 Million Sales Ahead of the Release of Its Second DLC, Dragon Ball Daima
Dragon Ball Z, Dragon Ball Kakarot, Dragon Ball Daima, Bandai Namco, CyberConnect 2, video game...
By نسرين أمل 2026-01-13 01:05:22 0 380
Art
A Misconfiguratie Die Bedrijfstreamingplatforms Achtervolgt, Kan Gevoelige Gegevens Blootleggen
misconfiguratie, bedrijfstreamingplatforms, gevoelige gegevens, beveiliging, API-configuraties,...
By Tom Pepijn 2025-08-10 01:05:20 1 1K
Crafts
Google Business Profile: A Comprehensive Guide to Optimizing Your Business Presence
Google Business Profile, Google My Business, local SEO, business listings, customer reviews,...
By جود أمل 2026-01-26 01:05:27 0 42
Coding
From Zero to Code: Installing Node.js & NPM on Debian 11
If you're exploring JavaScript development on the backend, Node.js and NPM (Node Package Manager)...
By Heman Jone 2025-07-18 14:46:15 0 1K
Jocuri
How to Secure the Ultra-Rare Comp Ball in Pokémon Legends ZA Before Time Runs Out
Pokémon Legends ZA has been keeping its players on their toes with exciting new rewards...
By Olivia OliviaGriff 2025-11-05 07:30:20 0 5K
Sponsor
Virtuala FansOnly https://virtuala.site