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
الأقسام
إقرأ المزيد
أخرى
Take the Perfect Girlfriend Experience from the Independent Aundh Call Girls
Having sex is thought to be biologically necessary for all humans, including women. There is one...
بواسطة Kriti Apte 2025-07-25 10:35:22 0 144
أخرى
Vegan Steak Market Size, Share, and Global Industry Forecast
Market Overview The global vegan steak market is experiencing significant momentum as consumer...
بواسطة MAYUR YADAV 2025-09-10 10:13:30 0 13
أخرى
Nevjerojatni prizori, Kim snimljen na bazenu: Pazite tek što se nalazi na stoliću pored
Nevjerojatni prizori, Kim snimljen na bazenu: Pazite tek što se nalazi na stoliću pored...
بواسطة Vedrana Kašić 2025-06-26 12:56:10 0 189
أخرى
Apple's first foldable could make the iPhone exciting again – but it needs this one feature
Apple's first foldable could make the iPhone exciting again – but it needs this one...
بواسطة Augestina Powers 2025-07-13 08:18:11 0 66
Art
USDA Libera Financiamento de Agricultura para Escolas Após Cancelações Anteriores
financiamento agricultura, USDA, programa escola, agricultura para escolas, investimento na...
بواسطة Gabriel Rogério 2025-09-12 22:05:52 1 44
إعلان مُمول
Virtuala FansOnly https://virtuala.site