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
Κατηγορίες
Διαβάζω περισσότερα
Art
3D Gaussian Splats werden zum glTF-Standard hinzugefügt: Eine Katastrophe in der digitalen Welt
3D-Gaussian-Splats, glTF-Standard, Khronos Group, Open Geospatial Consortium, 3D-Daten, digitale...
από Hannah Elisa 2025-08-29 07:05:31 1 161
Art
# 《招魂4》创造恐怖电影历史上最大的首个周末票房
招魂4, 恐怖电影, 票房, 历史, 电影, 全球票房, 观众, 反响, 评价, 电影产业...
από Peng Wan 2025-09-08 21:05:18 1 294
άλλο
Competitive Landscape and Future Outlook of North America’s Roofing Materials Industry
The North America Residential and Commercial Roofing Materials Market size was valued at USD...
από MAYUR YADAV 2025-09-02 11:46:47 0 135
άλλο
Phone Number Extractor - Chrome Extension
Extracting phone numbers from any website is now easy with Phone Number Extractor, a free Chrome...
από Shabirkhan 7sk 2025-04-21 06:05:58 0 534
άλλο
BFSI Workforce Planning: Adapting to Digitalization and Changing Job Roles
A new report on the Banking, Financial Services, and Insurance (BFSI) job market across the U.S.,...
από Adam Cooper 2025-04-03 02:45:32 0 570
Προωθημένο
Virtuala FansOnly https://virtuala.site