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
Camera und ChArUco: Halten Sie die Verzerrungen aus Ihren 3D-Drucken fern!
3D-Druck, CNC-Maschinen, Kalibrierung, Verzerrung, ChArUco, Laser Cutter, Präzision, Technologie,...
От Lorena Ella 2025-09-06 21:05:16 1 67
Art
USDA Plant, Union Verträge für Fleisch- und Geflügelinspektoren zu Kündigen
USDA, Fleischinspektion, Lebensmittelsicherheit, Gewerkschaften, Arbeitsrecht, Tierschutz,...
От Nika Sophie 2025-08-15 17:05:30 1 47
Causes
Strong Packaging Starts with the Right Testing – LabZenix Box Compression Tester
Every product deserves packaging that protects it until it reaches the customer. But how do you...
От Labzenix Instruments 2025-08-21 15:43:06 0 80
Другое
Luxury Vinyl Tile Plank Market Insights: Trends, Growth, and Forecast to 2032
Luxury Vinyl Tile Plank Market Overview: Luxury Vinyl Tile (LVT) planks are designed to...
От Cassie Tyler 2024-12-06 10:21:24 0 465
Art
Échange instantané de visages avec EaseMate AI : Mèmes und VR-Avatare, die Spaß machen
AI, Mèmes, VR-Avatare, Gesichtsaustausch, Technologie, EaseMate, Spaß, 2025, virtuelle Realität,...
От Leni Greta 2025-08-20 11:05:24 1 87
Спонсоры
Virtuala FansOnly https://virtuala.site