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

إعلان مُمول
إعلان مُمول
إعلان مُمول
إعلان مُمول
إعلان مُمول
البحث
إعلان مُمول
Virtuala FansOnly
CDN FREE
Cloud Convert
الأقسام
إقرأ المزيد
أخرى
Southeast Asia Used Cars Market Growth, Segment, Analysis & Forecast Report, 2033 | UnivDatos
According to a new report by UnivDatos the Southeast Asia Used Cars Market is expected...
بواسطة Ahasan Ali 2025-12-23 10:42:48 0 694
أخرى
Companies adopt stacker cranes to optimize warehouse space and minimize operational costs
Polaris Market Research has introduced the latest market research report titled Stacker Crane...
بواسطة MAYUR YADAV 2025-11-25 11:54:25 0 394
أخرى
Smart Fitness Mirror Market Report, Segments, Share, Trends & Forecast 2033
Smart fitness mirrors are just like the conventional mirror with a built-in LCD or OLED display...
بواسطة Biswajit Swain 2025-12-23 08:25:03 0 799
Health
Beyond Medication: How Homeopathy Can Support ADHD in Children
Attention-Deficit/Hyperactivity Disorder (ADHD) is one of the most common neurodevelopmental...
بواسطة Autism Help 2025-11-03 11:11:11 0 1كيلو بايت
أخرى
Navigating the Final Stretch: Your Guide to NURS FPX 4065, 4015, and 4905 Assessments
Navigating the Final Stretch: Your Guide to NURS FPX 4065, 4015, and 4905 Assessments Completing...
بواسطة Jerry Williams 2025-07-16 12:22:12 0 2كيلو بايت
إعلان مُمول
Virtuala FansOnly https://virtuala.site