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

حمایت‌شده
حمایت‌شده
حمایت‌شده
جستجو
Virtualbook
CDN FREE
دسته بندی ها
ادامه مطلب
Art
Влияет ли ИИ на угнетение женщин?
## Влияет ли ИИ на угнетение женщин? Время от времени мы оказываемся на грани нового мира, где...
توسط أناتولي Никита 2025-09-09 19:05:34 1 49
Shopping
Products Categories – Gould Solenoid Valves
Industrial and commercial solenoid valves sales categories including: steam, gas, oil, gasoline,...
توسط Shabirkhan 7sk 2024-12-03 06:49:26 0 592
صفحه اصلی
Entrümpelung & Haushaltsauflösung Bonn 02241-2664987
Auch Bonn gehört bei Entrümpelung & Haushaltsauflösung zu unserem...
توسط Shabirkhan 7sk 2025-05-06 07:59:37 0 284
دیگر
Lightweight Materials and Innovations in Aircraft Fuel Systems Market
According to the recent analysis by Polaris Market Research, the Aircraft Fuel Systems Market...
توسط MAYUR YADAV 2025-09-01 13:29:33 0 15
Health
Machine Scanner for Veins
Revolutionizing Healthcare: Machine Scanner for Veins & Portable Ultrasound Machine...
توسط Sharif Khan 2025-08-20 04:51:47 0 206
حمایت‌شده
Virtuala FansOnly https://virtuala.site