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

0
883

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

Patrocinado
Patrocinado
Patrocinado
Patrocinado
Patrocinado
Pesquisar
Patrocinado
Virtuala FansOnly
CDN FREE
Cloud Convert
Categorias
Leia Mais
Outro
Mortgage-Backed Securities Market Outlook, Size, Share, Trends & Research Report, 2033 | UnivDatos
According to UnivDatos analysis, rising global demand for housing finance, coupled with...
Por Ahasan Ali 2025-11-25 11:22:51 0 132
Food
donate food online
donate food online   Hunger remains one of the most pressing challenges faced by vulnerable...
Por David Kimm 2026-01-25 07:26:13 0 52
Art
免费下载:硬表面PBR纹理
硬表面纹理, PBR纹理, Blender, 4K纹理, 无缝纹理, 资产下载, 3D建模, 纹理下载 ## 硬表面PBR纹理的完美选择:给你的3D建模带来一丝新鲜感...
Por Dan Fu 2025-09-15 09:05:19 1 2K
Art
Camera und ChArUco: Halten Sie die Verzerrungen aus Ihren 3D-Drucken fern!
3D-Druck, CNC-Maschinen, Kalibrierung, Verzerrung, ChArUco, Laser Cutter, Präzision, Technologie,...
Por Lorena Ella 2025-09-06 21:05:16 1 1K
Music
Improving Sticky Posts: Unlocking the Potential of Featured Content in WordPress
sticky posts, featured content, WordPress, blogging tips, content visibility, sticky posts...
Por نبيل ناصر 2026-01-20 19:05:23 0 138
Patrocinado
Virtuala FansOnly https://virtuala.site