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

Sponsorluk
Sponsorluk
Sponsorluk
Sponsorluk
Sponsorluk
Site içinde arama yapın
Sponsorluk
Virtuala FansOnly
CDN FREE
Cloud Convert
Kategoriler
Read More
Food
Mold Inhibitors Market Outlook by Key Player, Statistics, Revenue, and Forecast 2032
Mold Inhibitors Market Overview: Mold Inhibitors Market Projected to Reach USD 2.5 Billion By...
By Cassie Tyler 2025-02-27 04:41:55 0 371
Home
Asbestsanierung in Bottrop 0231-98194868
Von A wie Asbesterkennung bis S wie Schadstoffanalyse. Wir helfen fachmännisch nach der TRGS...
By Shabirkhan 7sk 2025-07-31 09:03:35 0 81
Live Stream
Live streaming
Live media
By Drago Merkaš 2025-01-17 12:54:41 0 690
Home
Entrümpelung & Haushaltsauflösung Niederkassel 02241-2664987
Auch Niederkassel gehört bei Entrümpelung & Haushaltsauflösung zu unserem...
By Shabirkhan 7sk 2025-08-06 06:02:59 0 413
Other
Engine Control Modules Market Poised for Robust Growth Driven by Electrification and Advanced Automotive Electronics
The global Engine Control Modules Market is entering a phase of rapid transformation as...
By Riya Sharma 2025-09-29 10:51:07 0 172
Sponsorluk
Virtuala FansOnly https://virtuala.site