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

Patrocinados
Patrocinados
Patrocinados
Buscar
Virtualbook
CDN FREE
Categorías
Read More
Coding
HTML Email Accessibility Report 2025
HTML Email Accessibility Report 2025 | CSS-Tricks Some weekend reading on...
By Luka Matijević 2025-05-31 20:21:45 0 280
Art
睡眠专注耳机的奇迹:我最爱的音频品牌新发布,我的真实体验
耳机, 睡眠耳机, 音频品牌, 休息, 听觉体验, 耳机评测, 音乐, 生活方式, 睡眠质量, 科技产品 ## 引言...
By Zhong Qing 2025-09-04 11:05:18 1 48
Life
FOTO/VIDEO Napadnuta bolnica u Izraelu, Netanyahu bijesan: "Natjerat ćemo tiranine iz Teherana da plate"
FOTO/VIDEO Napadnuta bolnica u Izraelu, Netanyahu bijesan: "Natjerat ćemo tiranine iz Teherana da...
By Augestina Powers 2025-06-19 09:50:03 0 209
Home
Google soutient Gentle Monster avec un énorme investissement !
Google soutient Gentle Monster avec un énorme investissement ! Google prévoit de lancer des...
By Mary Fourth 2025-06-29 09:08:02 0 157
Art
آبل تقاضي أوبو لسرقتها معلومات سرية بطريقة غير مشروعة!
آبل, أوبو, معلومات سرية, نزاع قانوني, تقنية, حقوق الملكية الفكرية, انتهاك الخصوصية, سرقة معلومات...
By Laura Anna 2025-08-25 03:05:15 1 62
Patrocinados
Virtuala FansOnly https://virtuala.site