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

0
451

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
Art
Die verborgenen Zutaten hinter der Kreativität von KI
Kreativität, Künstliche Intelligenz, Bildgeneratoren, Trainingsdaten, Architektur, Technologie,...
By Sophie Leni 2025-08-25 04:05:30 1 176
Alte
Foam Stabilizer Additives Market Expected to Expand with Rising Demand in Food, Cosmetics, and Industrial Applications
The Foam Stabilizer Additives Market is experiencing notable growth as demand surges across food...
By Riya Sharma 2025-10-03 12:06:52 0 160
Jocuri
Download Teen Patti Master Old Version for a Classic Gaming Experience
Looking to revisit the original thrill of Teen Patti? The Teen Patti Master Old Version brings...
By Teenpatti Masteroldversion 2025-04-11 11:48:04 0 829
Food
Cocoa Butter Alternatives Market Outlook with Investment, Gross Margin, and Forecast 2032
Cocoa Butter Alternatives Market Overview The global Cocoa Butter Alternatives Market is...
By Cassie Tyler 2025-02-27 05:24:10 0 436
Sponsor
Virtuala FansOnly https://virtuala.site