如何在数据库中随机字母列表的每个字母后添加逗号

时间:2016-08-25 09:13:52

标签: mysql sql replace comma

如果我的所有记录都有随机字母,如何在每封信后添加逗号?

我有什么:

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

2 个答案:

答案 0 :(得分:3)

这些操作应该通过应用程序级别而不是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上运行

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

您可以为整个表格执行此操作,并且如果需要,您也可以轻松地进行更新。

答案 1 :(得分: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;

测试表:

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