How to replace every other instance of a particular character in a MySQL string?

前端 未结 5 1261
梦毁少年i
梦毁少年i 2021-01-07 22:49

How to replace value in mysql column by query like, Column is options and its of type varchar(255)

From

id   options
1    A         


        
5条回答
  •  暖寄归人
    2021-01-07 23:18

    Hum, I think you are trying to do something like this

    SELECT GROUP_CONCAT(CONCAT(options,",") SEPARATOR "|") FROM Table.name;
    

    I explain briefly, I take for each row the result and I concatenate "," and I concatenate all the row with the separator "|". You will have to change the Table.name with the name of your table

    If you want to concatenate one more value like A,B,C (you did not explain from where the ABC value are coming from so let's say ValueWhereABCisComingFrom):

    SELECT GROUP_CONCAT(CONCAT(ValueWhereABCisComingFrom,"|",options) SEPARATOR ",") FROM Table.name;
    

    if my table is like this :

    id | ValueWhereABCisComingFrom | options
    0  | A    | 10
    1  | B    | 20
    2  | C    | 30
    

    You wil have something like that :

    A|10,B|20,C|30
    

    EDIT 1

    There is no way to do that in that case. There is no function like preg_replace in mysql. All you can do is to replace all the "|" like

    SELECT  Replace(options, '|', ',') AS P
    FROM `docs`;
    

    In MariaDB, there is such a function so you could maybe try to pass from one base to an other. But with MYSQL only, no way :/

提交回复
热议问题