请教Oracle中字符排序问题。谢谢

saintKnight 2006-10-28 10:28:02
现在数据库的数据为:
1
1-1
1-2
1-1-1
2
2-1
3
4
5
6
7
8
9
10
11
……

需要请上述内容按升序排序成下列结果:
1
1-1
1-1-1
1-2
2
2-1
3
4
5
6
7
8
9
10
11
……
请问有什么好方法?谢谢
...全文
220 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
saintKnight 2006-11-09
  • 打赏
  • 举报
回复
还有没有更好的办法?
sungangsg 2006-10-28
  • 打赏
  • 举报
回复
还是写个函数吧,用来把1-1-1转换成数字111,然后以这个函数排序就行了,比写一个复杂的查询语句简单多了,多角度思维啊
mosaic 2006-10-28
  • 打赏
  • 举报
回复
select a, b from (
select a, a b from xx where lengthb(a)>1 and substrb(a,2,1)>='1' and substrb(a,2,1)<='9' union select a, '0'||a b from xx where lengthb(a)=1 union select a,'0'||a b from xx where lengthb(a)>1 and (substrb(a,2,1)<'1' or substrb(a,2,1)>'9') ) order by b
mosaic 2006-10-28
  • 打赏
  • 举报
回复
select a, b from (
select a, '0'||a b from xx where lengthb(a)>1 and substrb(a,2,1)>='1' and substrb(a,2,1)<='9' union select a, '0'||a b from xx where lengthb(a)=1 union select a,a b from xx where lengthb(a)>1 and (substrb(a,2,1)<'1' or substrb(a,2,1)>'9') ) order by b

只用a列即可。
但只对100以下有效,且-后面的数字也只能到9,呵呵。
GnuDoyng 2006-10-28
  • 打赏
  • 举报
回复
能不能在个位数之前加“0”?
saintKnight 2006-10-28
  • 打赏
  • 举报
回复
这样是不行的,不知是测试过没有。出来的结果是:
1
1-1
1-1-1
1-2
10
11
2
3
4
5
6
7
8
9
galant2008 2006-10-28
  • 打赏
  • 举报
回复
select * from 表名order by 要排序的字段

17,090

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧