Mysql remove the specific word in comma seperated string

后端 未结 6 800
温柔的废话
温柔的废话 2020-12-20 03:35

My table had \'tags\' field like this:

Tags

tag1,tag2,tag3

How to remove \',tag2\' including comma using mysql query.

6条回答
  •  不知归路
    2020-12-20 04:18

    A quick tip:

    UPDATE sets SET tags = tags &~ (1 << FIND_IN_SET('tag2', tags) - 1);
    

    Does not work when your set looks like: 'tag1','tag2','tag3'

    'tag3' will get lost by this operation

    I ended up using a simple double replace:

    UPDATE sets SET tags = replace(replace(tags, 'tag2', ''), ',,', '')
    

    The wrapping replace removes left over comma's from replacing just 'tag2', as you can't be sure if your tagg will be the first or last item of the set

    regarding the case of tag22 leaving a 2, doing it without leading zero's as the OP did, your bound for trouble anyway

提交回复
热议问题