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

Gesponsert
Gesponsert
Gesponsert
Gesponsert
Gesponsert
Suche
Gesponsert
Virtuala FansOnly
CDN FREE
Cloud Convert
Kategorien
Mehr lesen
Andere
Trusted Market Research Company for Strategic Insights – UnivDatos
In today’s fast-changing business world, organizations cannot depend on outdated...
Von Biswajit Swain 2025-11-18 09:55:01 0 817
Andere
Global Laboratory Freezers Market Analysis and Insights
Market OverviewAccording To The Research Report Published By Polaris Market Research, The Global...
Von MAYUR YADAV 2025-09-25 10:45:55 0 553
Religion
Lazy Composer 2 Adds One-Click Film Emulation to Blender: Revolutionizing Cinematic Color Grading
Blender, DaVinci Resolve, film emulation, color grading, cinematic effects, visual effects,...
Von ألكسندرا زويا 2026-01-28 03:05:41 0 27
Andere
Why Yellow Flash Media is Your Ideal Partner for PPC Advertising Success
In the ever-evolving world of digital marketing, Yellow Flash Media stands out as a trusted name...
Von Yellow Flashmedia 2025-06-13 15:34:14 0 2KB
Art
Amiga Programming in 2025 with AmiBlitz
## L'Amiga e il suo Mondo: Un Ritorno Inaspettato Se pensavi che l'era dei microcomputer Amiga...
Von Daniele Matteo 2025-08-11 01:05:34 1 1KB
Gesponsert
Virtuala FansOnly https://virtuala.site