用MySQL替换逗号分隔结果上的数字?

为此,将CASE语句与FIND_IN_SET()一起使用。让我们首先创建一个表-

mysql> create table DemoTable1629

    -> (

    -> Month varchar(100)

    -> );

使用insert命令在表中插入一些记录。

mysql> insert into DemoTable1629 values('2,4,6');

mysql> insert into DemoTable1629 values('1,3,5,12');

mysql> insert into DemoTable1629 values('7,8,9,10');

使用select语句显示表中的所有记录-

mysql> select * from DemoTable1629;

这将产生以下输出-

+----------+

| Month    |

+----------+

| 2,4,6    |

| 1,3,5,12 |

| 7,8,9,10 |

+----------+

3 rows in set (0.00 sec)

以下是替换逗号分隔结果上的数字的查询-

mysql> select trim(trailing ',' from concat(

    ->     CASE WHEN FIND_IN_SET(1, Month) THEN 'January,' ELSE '' END,

    ->     CASE WHEN FIND_IN_SET(2, Month) THEN 'Feburary,' ELSE '' END,

    ->     CASE WHEN FIND_IN_SET(3, Month) THEN 'March,' ELSE '' END,

    ->     CASE WHEN FIND_IN_SET(4, Month) THEN 'April,' ELSE '' END,

    ->     CASE WHEN FIND_IN_SET(5, Month) THEN 'May,' ELSE '' END,

    ->     CASE WHEN FIND_IN_SET(6, Month) THEN 'June,' ELSE '' END,

    ->     CASE WHEN FIND_IN_SET(7, Month) THEN 'July,' ELSE '' END,

    ->     CASE WHEN FIND_IN_SET(8, Month) THEN 'August,' ELSE '' END,

    ->     CASE WHEN FIND_IN_SET(9, Month) THEN 'September,' ELSE '' END,

    ->     CASE WHEN FIND_IN_SET(10,Month) THEN 'October,' ELSE '' END,

    ->     CASE WHEN FIND_IN_SET(11,Month) THEN 'November,' ELSE '' END,

    ->     CASE WHEN FIND_IN_SET(12,Month) THEN 'December,' ELSE '' END

    -> ))

    -> from DemoTable1629;

这将产生以下输出-

+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| trim(trailing ',' from concat(

    CASE WHEN FIND_IN_SET(1, Month) THEN 'January,' ELSE '' END,

    CASE WHEN FIND_IN_SET(2, Month) THEN 'Feburary,' ELSE '' END,

    CASE WHEN FIND_IN_SET(3, Month) THEN 'Marh,' ELSE '' END,

    CASE WHEN FIND_IN_SET(4, Mon |

+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Feburary,April,June                                                                                                                                                                                                                                                |

| January,March,May,December                                                                                                                                                                                                                                        |

| July,August,September,October                                                                                                                                                                                                                                      |

+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

3 rows in set (0.04 sec)

以上是 用MySQL替换逗号分隔结果上的数字? 的全部内容, 来源链接: utcz.com/z/331313.html

回到顶部