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

0
882

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
دسته بندی ها
ادامه مطلب
دیگر
Global Guitar Speaker Market: Size, Share, and Forecast Analysis to 2032
Guitar Speaker Market Overview: Guitar speakers are specialized loudspeakers used in guitar...
توسط Cassie Tyler 2025-02-03 07:30:31 0 779
Religion
Tutorial: Simulating Crowds with Golaem & Maya
## Introduction In the realm of visual effects (VFX) and game cinematics, crowd simulation is...
توسط Frieda Emilia 2026-01-19 05:05:24 0 194
Networking
Custom Software & App Development Company in Indianapolis IN
Top software development company in Indianapolis offering custom software, mobile app...
توسط Shabirkhan 7sk 2026-01-26 11:24:10 0 16
Drinks
Vidéos de formation Top Designer: Unlocking Your Creative Potential
top designer training videos, design tutorials, E-NOVATIONS, creative learning, online design...
توسط Laura Lena 2026-01-28 05:05:25 0 24
حمایت‌شده
Virtuala FansOnly https://virtuala.site