同一字段多次排序的问题

wenhongL 2007-07-19 02:29:34
select * from table
order left(col1,1),right(col1,2)

提示:
服务器: 消息 169,级别 15,状态 1,行 1
在排序依据列表中多次指定了某一列。排序依据列表中的列必须是唯一的。
...全文
594 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
wenhongL 2007-07-19
  • 打赏
  • 举报
回复
谢谢,这个方法可行。
回复人:paoluo(一天到晚游泳的鱼) ( 四星(高级)) 信誉:100 2007-07-19 15:06:16 得分:0
?
try


Select 列出除了col12, col13外的所有列名
From(select *, cast(dbo.split(col1,2) as int) As col12, cast(dbo.split(col1 ,3) as int) As col13 from maps_all) A
Order By col12, col13
paoluo 2007-07-19
  • 打赏
  • 举报
回复
order by後面的本來就是不確定的。

另外,我這是舉例在普通的語句中是沒有問題的,但是order by中使用函數沒有測試,你試這個吧。

Select 列出除了col12, col13外的所有列名
From(select *, cast(dbo.split(col1,2) as int) As col12, cast(dbo.split(col1 ,3) as int) As col13 from maps_all) A
Order By col12, col13
wenhongL 2007-07-19
  • 打赏
  • 举报
回复
这样不行,order by 后面不能固定

回复人:paoluo(一天到晚游泳的鱼) ( 四星(高级)) 信誉:100 2007-07-19 15:04:26 得分:0
?
一樣沒問題的

Create Table [table]
(col1 Varchar(100))
Insert [table] Select 'A-12-156'
Union All Select 'A-8-25'
Union All Select 'A-8-27'
GO
select * from [table]
order By left(col1,1),right(col1,2)
GO
Drop Table [table]
--Result
/*
col1
A-8-25
A-8-27
A-12-156
*/
paoluo 2007-07-19
  • 打赏
  • 举报
回复
try


Select 列出除了col12, col13外的所有列名
From(select *, cast(dbo.split(col1,2) as int) As col12, cast(dbo.split(col1 ,3) as int) As col13 from maps_all) A
Order By col12, col13
liulcster 2007-07-19
  • 打赏
  • 举报
回复
select * from table
order by left(col1,1),right(col1,2)
paoluo 2007-07-19
  • 打赏
  • 举报
回复
一樣沒問題的

Create Table [table]
(col1 Varchar(100))
Insert [table] Select 'A-12-156'
Union All Select 'A-8-25'
Union All Select 'A-8-27'
GO
select * from [table]
order By left(col1,1),right(col1,2)
GO
Drop Table [table]
--Result
/*
col1
A-8-25
A-8-27
A-12-156
*/
wenhongL 2007-07-19
  • 打赏
  • 举报
回复
如果col1 的值是这样的
A-12-156
A-265-1
A-8-25


回复人:paoluo(一天到晚游泳的鱼) ( 四星(高级)) 信誉:100 2007-07-19 14:38:12 得分:0
?
沒有問題啊。

Create Table [table]
(col1 Varchar(100))
Insert [table] Select 'A112'
Union All Select 'B113'
Union All Select 'D125'
Union All Select 'C136'
Union All Select 'A128'
GO
select * from [table]
order By left(col1,1),right(col1,2)
GO
Drop Table [table]
--Result
/*
col1
A112
A128
B113
C136
D125
*/
wenhongL 2007-07-19
  • 打赏
  • 举报
回复
split 函数就是分割类似 A-111-11 的字符串。
wenhongL 2007-07-19
  • 打赏
  • 举报
回复
select * from maps_all where (1=1) order by cast(dbo.split(col1,2) as int), cast(dbo.split(col1 ,3) as int)

这是我实际的语句。
echiynn 2007-07-19
  • 打赏
  • 举报
回复
....

剛試了下,這句也沒問題
select * from titles
order by left(title_id, 1), right(title_id, 1)
paoluo 2007-07-19
  • 打赏
  • 举报
回复
chuifengde(树上的鸟儿) ( ) 信誉:100 2007-07-19 14:38:23 得分: 0


order by --少了by


---------
應該不是這個原因,否則錯誤提示不是這樣的。
paoluo 2007-07-19
  • 打赏
  • 举报
回复
貼出你的實際語句看看
chuifengde 2007-07-19
  • 打赏
  • 举报
回复
order by --少了by
paoluo 2007-07-19
  • 打赏
  • 举报
回复
沒有問題啊。

Create Table [table]
(col1 Varchar(100))
Insert [table] Select 'A112'
Union All Select 'B113'
Union All Select 'D125'
Union All Select 'C136'
Union All Select 'A128'
GO
select * from [table]
order By left(col1,1),right(col1,2)
GO
Drop Table [table]
--Result
/*
col1
A112
A128
B113
C136
D125
*/
echiynn 2007-07-19
  • 打赏
  • 举报
回复
你這一列又要按左邊的排,又要按右邊的排,別人當然不樂意了,呵呵
wgqqgw 2007-07-19
  • 打赏
  • 举报
回复
oh gosh, 看错了,不好意思哈,飘过……
wgqqgw 2007-07-19
  • 打赏
  • 举报
回复
大哥,这个提示是让你select子句里要包含order by子句中出现的字段

select left(col1,1),right(col1,2),* from table
order left(col1,1),right(col1,2)

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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