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
Κατηγορίες
Διαβάζω περισσότερα
άλλο
The Capella FlexPath Nursing Assessments Codes: An Interpretation Guide for Students
Students may learn at their own pace with the self-paced learning experience provided by Capella...
από Kajohan Will 2025-07-16 22:01:45 0 1χλμ.
Παιχνίδια
Microsoft Accelerates File Protection with Hardware-Enhanced BitLocker
BitLocker, hardware acceleration, file protection, Microsoft, NVMe storage, data security,...
από Tao Ping 2026-01-17 11:05:21 0 248
άλλο
Mutual Fund Investment Planner – inXits
Investing wisely requires guidance, and that’s where a trusted mutual fund investment...
από InXits Com 2026-01-14 16:04:10 0 59
άλλο
Nasal Vaccines Market Size Projected to Reach USD 876.2 Million by 2032
The global Nasal Vaccines Market comprises prophylactic products designed for administration...
από Shiv Mehara 2025-12-16 05:20:39 0 577
άλλο
Test Smarter. Seal Stronger. Trust LabZenix Secure Seal Tester
Ensure every product you package is perfectly sealed and protected with the Secure Seal Tester...
από Labzenix Instruments 2025-10-10 16:55:36 0 1χλμ.
Προωθημένο
Virtuala FansOnly https://virtuala.site