简体   繁体   中英

mysql remove unwanted text from field using substring_index with wildcard

I have a table called hamburgers. I want to clean up the data and remove some garbage text from around the name of the hamburger and just use ONLY the name of the hamburger in the title field. So far this is all my little brain can muster...

UPDATE hamburgers
SET title = SELECT SUBSTRING_INDEX(title, "My favorite hamburger is called %, I'm not really sure why.   Yesterday was the first time I ate a %", -1) FROM hamburgers
where title like "%My favorite hamburger is called %.%";

I suck at MySql but I just want to figure out a way to remove the text surrounding the name of the burger. There are 3,000 or so records that have the same exact text surrounding the name of the hamburger. TIA.

This might get you going in the right direction:

select substring_index(substring_index("My favorite hamburger is called Whopper, I'm not really sure why.   Yesterday was the first time I ate a Whopper","My favorite hamburger is called ",-1),",",1);

This will give you:

Whopper

So:

UPDATE hamburgers
SET title = substring_index(substring_index(title,"My favorite hamburger is called ",-1),",",1)
where title like "My favorite hamburger is called %,";

It does kinda rely on there being a comma after the hamburger though....

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM