Jump to content

Replace a value of comma separated string


watsmyname

Recommended Posts

Hey there

I m sorry if its a repost, i tried to search with no results.

 

I have mysql table and i have a field  which holds value something like sometimes 65, sometimes 65,34, sometimes 65,34,76 and so on. All i need is that i have to match certain value in these comma separated fields [finding i know, we use FIND_IN_SETS], but my problem is i have to find particular value and replace with another

 

For instance, i have to search 76 and its found in 65,34,76. Now i have to replace this 76 with 67 to make our set look like 65,34,67.

 

I hope my query is clear, can any body show me the way to achieve this with mysql?

 

Thanks

Link to comment
Share on other sites

You have to search for a complete value -- wrapped in commas -- and wrap your field value in commas as well.  That's why storing delimited values is a very bad idea.

 

update tablename SET fieldname=(select replace( CONCAT(',',fieldname,','), ',76,', ',67,')) WHERE FIND_IN_SET('76',fieldname)>0

Link to comment
Share on other sites

Thanks mate

 

My problem is that i have to work with database made by someone else long ago. The reason a field have comma separated value is because a song might have two artists, so in song table they've put the value in comma separated form.

 

Next your mysql code works, but it adds extra comma like ,176,65,34,67. And next time when i search for 176, its position will be 2 not 1, isn't there another work around??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.