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

Sponzorováno
Sponzorováno
Sponzorováno
Sponzorováno
Sponzorováno
Hledat
Sponzorováno
Virtuala FansOnly
CDN FREE
Cloud Convert
Kategorie
Číst více
Art
سعر البيتكوين 2025: تحليل شامل ومتابعة لحظية لحركة السوق
## مقدمة سعر البيتكوين في السنوات القادمة أصبح موضوعًا مهمًا للكثير من المستثمرين والمحللين. في...
Od Catarina Sofia 2025-08-23 03:05:23 1 660
Health
NURS FPX
NURS FPX is an essential part of every nursing student’s academic and professional journey,...
Od William Harrison 2025-10-31 19:15:45 0 1K
Bible
EZNPC: Affordable and Rare Grow a Garden Pets for You
Looking to expand your Grow a Garden collection with adorable, rare, and unique pets? EZNPC...
Od Jime Kalmiya 2026-01-27 02:07:01 0 12
Fitness
### Generation Z Waves the One Piece Flag to Sabotage Governments
One Piece, Generation Z, youth movements, symbolism, political activism, pop culture, anime...
Od Tobias Jonathan 2026-01-12 00:05:25 0 308
Music
IAs and Personas: Towards a New Extremity in Persona Design?
AI, personas, design methodologies, user experience, accessibility, inclusive design, artificial...
Od Mika Ole 2026-01-07 19:05:23 0 129
Sponzorováno
Virtuala FansOnly https://virtuala.site