I have a field called slug in my table with values in this format:
"username/campaign-name"
I would like to remove the values "username/" and remain with only "campaign-name".
I tried the following but stuck on how I can dynamically replace the string "hello"? Any other alternative method is appreciated.
UPDATE `campaigns`
SET `slug` = REPLACE(`slug`, `slug`,'hello')
WHERE `slug` IN (
SELECT `slug`
FROM (
SELECT `slug`
FROM `campaigns`
LIMIT 1
) slug
);
I would suggest using substring_index()
:
UPDATE `campaigns`
SET `slug` = substring_index(slug, '/', -1);
I'm not sure what the where
clause is supposed to do. If you only want to change one row, use limit
:
UPDATE `campaigns`
SET `slug` = substring_index(slug, '/', -1)
LIMIT 1;
For safety, you might want to verify the format of slug
:
UPDATE `campaigns`
SET `slug` = substring_index(slug, '/', -1)
WHERE slug like '%/%' and slug not like '%/%/%';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.