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
الأقسام
إقرأ المزيد
أخرى
Latin America Biochar Market Outlook, Size, Share, Trends & Research Report, 2033 | UnivDatos
According to UnivDatos, Soil regeneration and yield pressure in Agriculture and Regulated and...
بواسطة Ahasan Ali 2025-11-25 10:57:23 0 48
Art
Refugiados e a Terra da Esperança: O Impacto dos Cortes Federais nos Programas de Agricultura
refugiados, programas de agricultura, cortes federais, comunidade nepalense, esperança,...
بواسطة Lorenzo João 2025-08-18 21:05:27 1 396
Art
Building a Sci-Fi Stronghold in the Desert with Daniel Dana
## Ein futuristischer Traum in der Wüste: Daniel Dana und sein Sci-Fi-Stützpunkt Willkommen im...
بواسطة Niklas Emil 2025-08-22 01:05:16 1 243
Art
Butta Melta – Schluss mit dem Zerreißen von Toast durch harte Butter!
Butta Melta, Butter, Toast, Küchenhelfer, Butterverbreiter, Kücheninnovation, John Dingley,...
بواسطة Sara Lia 2025-08-26 09:05:22 1 87
إعلان مُمول
Virtuala FansOnly https://virtuala.site