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

0
309

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

Sponsor
Sponsor
Sponsor
Căutare
Virtualbook
CDN FREE
Categorii
Citeste mai mult
Shopping
Precision Washers & Shims Manufacturer | USA-Made Fastener Components – AMPG
AMPG is a leading manufacturer of precision washers and shims, producing high-quality, USA-made...
By Shabirkhan 7sk 2025-09-12 06:40:53 0 34
Alte
Nakon što su ih oteli i priveli iz voda uz obalu Egipta, Greta Thunberg i aktivisti će biti deportirani iz Izraela
Nakon što su ih oteli i priveli iz voda uz obalu Egipta, Greta Thunberg i aktivisti će biti...
By Martina Lukačić 2025-06-10 07:56:08 0 216
Art
The ultimate movie logos quiz
quiz de logos de filmes, cinema, cultura pop, testes de filmes, desafios de cinema ##...
By Catarina Sofia 2025-08-24 03:05:24 1 47
Art
**تشات جي بي تي يستخدم جوجل للبحث عن الويب بالتأكيد**
## مقدمة في زمنٍ يعج بالتكنولوجيا والابتكارات، أصبح الذكاء الاصطناعي جزءًا لا يتجزأ من حياتنا...
By مؤمن رامي 2025-08-07 00:05:38 1 38
Art
حزمة جديدة لألعاب ماريو تضم Mario Galexy 1,2 قادمة في 2 أكتوبر
## Einführung Die Welt von Mario, dem ikonischen Klempner aus dem Hause Nintendo, wird bald um...
By Nele Nika 2025-09-15 16:05:20 1 53
Sponsor
Virtuala FansOnly https://virtuala.site