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

Sponsor
Sponsor
Sponsor
Zoeken
Virtualbook
CDN FREE
Categorieën
Read More
Art
Rad Power Bikes RadRunner Max İncelemesi: Daha Hızlı ve Daha Güvenli!
Rad Power Bikes, RadRunner Max ile bisiklet tutkunlarına heyecan verici bir yenilik sunuyor. Bu...
By Okan Ege 2025-08-23 00:05:17 1 53
Art
UN Plastik Antlaşması Görüşmeleri Yine Başarısızlıkla Sonuçlandı
## Giriş Son dönemde yapılan Birleşmiş Milletler (BM) plastik antlaşması görüşmeleri, bir kez...
By Ece Kübra 2025-08-16 21:05:26 1 65
Art
NASA Sucht Freiwillige zur Verfolgung der Artemis II Mission
NASA, Freiwillige, Artemis Programm, Raumfahrt, Artemis II, Weltraummission, Raumfahrzeuge,...
By Ronja Alina 2025-09-01 04:05:14 1 55
Other
Trends and Future Opportunities in the Global Online Recruitment Technology Market
The global Online Recruitment Technology Market, valued at USD 12.98 billion in 2023, is...
By MAYUR YADAV 2025-09-02 11:50:25 0 30
Sports
Visuals and Presentation in EA FC 26 — Bringing Football to Life
If considering the visuals and presentation quality of EA FC 26, players might feel compelled to...
By Claus Oliver 2025-09-23 06:02:41 0 62
Sponsor
Virtuala FansOnly https://virtuala.site