2016-08-25 69 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

這些操作應該通過應用程序級而不是數據庫來完成。但是,您真的想從數據庫級別執行此操作,您可以使用用戶定義的函數輕鬆完成此操作。下面是一個函數來做到這一點

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

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> 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> 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  | 
+------+-----------------------+ 

Y 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; 

測試表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 

結果:

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 

對於現有的表使用查詢的更新值:

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 

Test on sqlfiddle.com

+0

也許我的問題是錯誤的,但我沒有4行。我有五萬行,全部用隨機字母。這是工作,但我不知道如何與每一行做到這一點。 – Tauras

+0

@Tauras我的答案應該適用於任意數量的行。 'seqnum'中的數字 - 是一個字符串上的字母位置,而不是輸入表格行的數量 – Mike

相關問題