2016-08-25 1 views
1

모든 레코드에 임의의 문자가있는 경우 모든 문자 뒤에 쉼표를 어떻게 추가 할 수 있습니까?데이터베이스에 임의의 임의 문자 목록의 각 문자 뒤에 쉼표를 추가하는 방법

내가 무엇을 가지고 :

1. AHGJTOSIYGJ 
2. OTPDBSKGY 
3. HFRYEC 
4. OPFKWIFS 
// etc 

내가 필요한 것 :

1. A, H, G, J, T, O, S, I, Y, G, J, 
2. O, T, P, D, B, S, K, G, Y, 
3. H, F, R, Y, E, C, 
4. O, P, F, K, W, I, F, S, 
// etc 
+1

[MySQL에서 정규 표현식 바꾸기] 가능한 중복? (http://stackoverflow.com/questions/986826/how-to-do-a-regular-expression-replace-in-mysql) – Jens

답변

3

이러한 작업은 DB가 아닌 응용 프로그램 수준을 통해 수행해야합니다. 그러나 DB 수준에서이 작업을 수행하려는 경우 사용자 정의 함수를 사용하여 쉽게 수행 할 수 있습니다. 다음은이

delimiter // 

create function myFunction(myString varchar(255)) 
returns varchar(255) 
begin 
declare strLen int ; 
declare lookupChar char(1); 
declare finalString varchar(255); 
declare x int; 

set strLen = length(myString); 
set x = 1 ; 
set finalString = ''; 
while x <= strLen do 
    set lookupChar = substring(myString,x,1); 
    if finalString = '' then 
    set finalString = lookupChar; 
    else 
    set finalString = concat(finalString,',',lookupChar); 
    end if; 
    set x = x+1; 
end while; 
return finalString; 
end// 

delimiter; 

을 할 수있는 기능은 기능 가

지금까지 너무 좋아, 이제를 사용하여 값을 선택할 수 있습니다

mysql> delimiter // 
mysql> create function myFunction(myString varchar(255)) 
    -> returns varchar(255) 
    -> begin 
    -> declare strLen int ; 
    -> declare lookupChar char(1); 
    -> declare finalString varchar(255); 
    -> declare x int; 
    -> 
    -> set strLen = length(myString); 
    -> set x = 1 ; 
    -> set finalString = ''; 
    -> while x <= strLen do 
    -> set lookupChar = substring(myString,x,1); 
    -> if finalString = '' then 
    ->  set finalString = lookupChar; 
    -> else 
    ->  set finalString = concat(finalString,',',lookupChar); 
    -> end if; 
    -> set x = x+1; 
    -> end while; 
    -> return finalString; 
    -> end// 
Query OK, 0 rows affected (0.00 sec) 

mysql> delimiter ; 
을 만들 이제 mysql을

mysql> create table mytable (id int, value varchar(100)); 
Query OK, 0 rows affected (0.19 sec) 

mysql> insert into mytable values (1,'AHGJTOSIYGJ'),(2,'OTPDBSKGY'),(3,'HFRYEC'),(4,'OPFKWIFS'); 
Query OK, 4 rows affected (0.02 sec) 

mysql> select * from mytable ; 
+------+-------------+ 
| id | value  | 
+------+-------------+ 
| 1 | AHGJTOSIYGJ | 
| 2 | OTPDBSKGY | 
| 3 | HFRYEC  | 
| 4 | OPFKWIFS | 
+------+-------------+ 
4 rows in set (0.00 sec) 

에서이 작업을 실행할 수 있습니다하자입니다 기능

mysql> select id,myFunction(value) as value from mytable ; 
+------+-----------------------+ 
| id | value     | 
+------+-----------------------+ 
| 1 | A,H,G,J,T,O,S,I,Y,G,J | 
| 2 | O,T,P,D,B,S,K,G,Y  | 
| 3 | H,F,R,Y,E,C   | 
| 4 | O,P,F,K,W,I,F,S  | 
+------+-----------------------+ 

ou는 전체 테이블에 대해이 작업을 수행 할 수 있으며 필요할 경우 쉽게 업데이트를 수행 할 수 있습니다.

1

우리는 최대 길이 입력 문자열 1에서 시퀀스 번호의 작업 테이블이 필요합니다

create table seqnum(X int not null, primary key(X)); 
insert into seqnum values(1),(2),(3),(4); 
insert into seqnum select X+4 from seqnum; 
insert into seqnum select X+8 from seqnum; 
insert into seqnum select X+16 from seqnum; 
insert into seqnum select X+32 from seqnum; 

테스트 tabl E :

create table test6(id int, lett varchar(100)); 
insert into test6 values(1,'AHGJTOSIYGJ'),(2,'OTPDBSKGY'),(3,'HFRYEC'),(4,'OPFKWIFS'); 

쿼리 테이블 TEST6에 변경된 문자를 선택합니다 :

select id, lett, group_concat(substr(lett,S.X,1) ORDER BY T.id, S.x SEPARATOR ', ') 
    from test6 T, seqnum S 
where S.X<=length(T.lett) 
group by id 

결과 :

update test6 A 
    join (
     select id, group_concat(substr(lett,S.X,1) ORDER BY T.id, S.x SEPARATOR ', ') new 
      from test6 T, seqnum S 
     where S.X>0 and S.X<=length(T.lett) 
     group by id 
     ) B ON A.id=B.id 
    set A.lett=B.new 

: 기존 테이블 사용 쿼리의 업데이트 값

1 | AHGJTOSIYGJ | A, H, G, J, T, O, S, I, Y, G, J 
2 | OTPDBSKGY | O, T, P, D, B, S, K, G, Y 
3 | HFRYEC  | H, F, R, Y, E, C 
4 | OPFKWIFS | O, P, F, K, W, I, F, S 

Test on sqlfiddle.com

+0

어쩌면 내 질문은 잘못되었지만 4 행이 없다. 나는 50k 행을 가지고 있으며 모두 무작위로 글자를 가지고있다. 이것은 작동하지만, 나는 모든 행에 이것을 어떻게하는지 모른다. – Tauras

+0

@ 토러스 내 대답은 여러 행에서 작동해야합니다. 'seqnum'에있는 숫자 - 한 문자열의 문자 위치이며, 입력 테이블 행 수는 아닙니다. – Mike