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

Sponsored
Sponsored
Sponsored
Sponsored
Sponsored
Search
Sponsored
Virtuala FansOnly
CDN FREE
Cloud Convert
Categories
Read More
Art
Blender-Entwickler-Notizen: Ein Blick hinter die Kulissen der kreativen Chaossteuerung
Blender, offene Software für 3D-Design, Animation und Rendering, hat mehr als nur die Herzen von...
By Lia Ella 2025-08-20 14:05:29 1 340
Food
Wine Market: Size, Share, and Forecast Analysis – 2032 Projections
Wine Market Overview: The intake of wine has increased globally due to its preference by...
By Cassie Tyler 2024-12-06 04:57:19 0 488
Literature
The Complete Guide to NURS FPX 6422: Expert Help for All Assessments
Introduction: Navigating Nursing Education Challenges Advanced nursing courses are demanding,...
By Zinia Smith 2025-06-26 21:27:39 0 523
Art
Перья — это фантастика, но они ставят инженеров в тупик
перья, инженеры, птицы, наука, вдохновение, технологии, биомиметика, природа ## Введение Перья...
By ليديا لاريسا 2025-08-30 00:05:14 1 178
Sports
5th-Yearly 'Purple Wednesday' Operate Delivered via GEHA Scheduled for September 4
The Kansas Metropolis Chiefs and Arrowhead Functions are internet hosting the 5th yearly Crimson...
By Cunane Cunane 2025-07-12 08:57:36 0 284
Sponsored
Virtuala FansOnly https://virtuala.site