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

0
968

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

Sponsored
Sponsored
Sponsored
Sponsored
Sponsored
Search
Sponsored
Virtuala FansOnly
CDN FREE
Cloud Convert
Categories
Read More
Art
ضغوط على ترامب لإلغاء صفقة بيع رقائق الذكاء الاصطناعي إلى الصين
## Einführung Die Welt steht am Rande eines neuen technologischen Krieges, und Donald Trump...
By Mia Sara 2025-08-17 11:05:32 1 841
Other
The $2 Million Question: When Procurement Becomes Your Secret Weapon
If you own a business, you might suddenly realize that almost 70% of your budget goes to buying...
By Ahasan Ali 2025-11-14 09:46:17 0 691
Other
Future of Drug Discovery: Role of Robots in the Pharmaceutical Market
The newly published study by Polaris Market Research, titled Pharmaceutical Robot Market, delves...
By MAYUR YADAV 2025-09-01 13:18:10 0 690
Dance
The Four Objectives of Data Storytelling: How to Make Your Data Tell a Story That Inspires Action
data storytelling, data visualization, communication strategies, inspire action, data-driven...
By Lola Élise 2026-01-18 20:05:27 0 400
Theater
Madrid Design Festival Revamps Competitions and Enhances Conditions
Madrid Design Festival, Design Competitions, Chamberí, Rediseña Madrid, Design Improvement,...
By Alessia Nicoletta 2026-02-02 00:05:25 0 458
Sponsored
Virtuala FansOnly https://virtuala.site