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

0
451

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
دسته بندی ها
ادامه مطلب
صفحه اصلی
48h Sanierung D´dorf Asbestkleber & Bodenbelag-0221-96986861
Floorflexplatten Düsseldorf - Von Floorflex, Cushion-Vinyl, asbesthaltigem Vinylboden bis...
توسط Shabirkhan 7sk 2024-12-02 07:49:05 0 915
Art
Wie man den Moiré-Effekt vor dem Drucken vermeidet
## Einleitung Hast du jemals eine schreckliche Druckausgabe gesehen, die dir die Stimmung...
توسط Clara Anna 2025-08-29 06:05:24 1 394
دیگر
Flight Planning and Dispatch Platforms Market is projected to reach $3.1 billion by 2033
Flight Planning and Dispatch Platforms Market is surging as airlines, cargo operators, and...
توسط Sadaf Sheikh 2025-10-15 09:47:27 0 165
دیگر
Middle East Solid State Transformer Market, Analysis, Trends, Growth and Forecast (2024-2032)
According to the UnivDatos, “Middle East Solid State Transformer Market” report, the...
توسط Praveen Gupta 2025-10-15 08:49:07 0 181
Art
Vendetta Forever – Ein minimalistisches VR-Shooter-Spiel
VR-Spiele, minimalistisch, Vendetta Forever, Shooter, Gaming, virtuelle Realität, Spieltest,...
توسط Victoria Emma 2025-08-15 10:05:22 1 248
حمایت‌شده
Virtuala FansOnly https://virtuala.site