Using WITH in sql query
Question
I am trying to replace #
with numbers, but the order by
is being ignored, and I was told to move the subquery usingWITH
or adding a limit to the order by
, I tried the latter but it didn't work, and I am not sure how to use WITH
in the following query.
update items
join (select id, @num := @num + 1 as num
from items, (select @num := 0) items
where parent_id = 159133 order by name) items2
on items2.id = items.id
set items.description = replace(items.description, '#' COLLATE utf8_unicode_ci, cast(num as char(10)))
WHERE parent_id = 159133 and type='photo';
Solution
Your query can be written without subqueries/joins like that:
UPDATE items
SET description = replace(description, '#', cast(@num:=@num+1 as char(10)))
WHERE parent_id = 159133
AND type='photo'
AND @num := 1 -- @num initialization by Akina
ORDER BY name ASC
;
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange