이를 위해 FIND_IN_SET()과 함께 CASE 문을 사용합니다. 먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable1629 -> ( -> Month varchar(100) -> ); Query OK, 0 rows affected (0.64 sec)
삽입 명령을 사용하여 테이블에 일부 레코드를 삽입하십시오.
mysql> insert into DemoTable1629 values('2,4,6'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1629 values('1,3,5,12'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1629 values('7,8,9,10'); Query OK, 1 row affected (0.15 sec)
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)