码迷,mamicode.com
首页 > 其他好文 > 详细

6.12 按字母顺序排列字符串

时间:2017-05-02 18:26:46      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:字符   group   select   问题   ack   war   bst   字母   rom   

问题:如下的结果集:

+--------+
| ename  |
+--------+
| ALLEN  |
| WARD   |
| MARTIN |
| BLAKE  |
| CLARK  |
| KING   |
| TURNER |
| JAMES  |
| MILLER |
+--------+

需要结果如下:

+--------+-----------------------------------------+
| ename  | group_concat(c order by c separator ‘‘) |
+--------+-----------------------------------------+
| ALLEN  | AELLN                                   |
| BLAKE  | ABEKL                                   |
| CLARK  | ACKLR                                   |
| JAMES  | AEJMS                                   |
| KING   | GIKN                                    |
| MARTIN | AIMNRT                                  |
| MILLER | EILLMR                                  |
| TURNER | ENRRTU                                  |
| WARD   | ADRW                                    |
+--------+-----------------------------------------+

 

解决方案:

select ename,group_concat(c order by c separator ‘‘)
from (
select ename,substr(a.ename,iter.pos,1) c
from emp a,
(select id pos from t10) iter
where iter.pos <=length(a.ename)
) x
group by ename;

 

+--------+-----------------------------------------+
| ename  | group_concat(c order by c separator ‘‘) |
+--------+-----------------------------------------+
| ALLEN  | AELLN                                   |
| BLAKE  | ABEKL                                   |
| CLARK  | ACKLR                                   |
| JAMES  | AEJMS                                   |
| KING   | GIKN                                    |
| MARTIN | AIMNRT                                  |
| MILLER | EILLMR                                  |
| TURNER | ENRRTU                                  |
| WARD   | ADRW                                    |
+--------+-----------------------------------------+

 

6.12 按字母顺序排列字符串

标签:字符   group   select   问题   ack   war   bst   字母   rom   

原文地址:http://www.cnblogs.com/liang545621/p/6797198.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!