日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

表中记录查询排序(设置排序规则)

發布時間:2025/7/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 表中记录查询排序(设置排序规则) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

--先創建一個表?
create table ai(?
id int not null,?
no varchar(10) not null?
)?
go?

--往表中插入數據?
insert into ai?
?select 105,'2'?
?union all?
?select 105,'1'?
?union all?
?select 103,'1'?
?union all?
?select 105,'4'?
go?

--查詢效果如下:?
select * from ai?
go?
id????????? no?????????
----------- ----------?
105???????? 2?
105???????? 1?
103???????? 1?
105???????? 4?


i.?
--要求的查詢結果如下?
--即要求no列的數據按'4','1','2'排列?
id????????? no?????????
----------- ----------?
105???????? 4?
105???????? 1?
103???????? 1?
105???????? 2



--解決方案1?
--利用函數CHARINDEX?
select * from ai?
?order by charindex(no,'4,1,2')?

--解決方案2?
--利用函數case?
select * from ai?
?order by case when no='4' then 1?
??????? when no='1' then 2?
????????????????????? when no='2' then 3?
???????????????? end?

--解決方案3?
--利用UNION 運算符?
select * from ai?
?where no='4'?
union all?
select * from ai?
?where no='1'?
union all?
select * from ai?
?where no='2'?

ii.?
--查詢要求指定no='4'排第一行,其他的行隨機排序?
id????????? no?????????
----------- ----------?
105???????? 4?
105???????? 2?
105???????? 1?
103???????? 1?

--解決方案?
select * from ai?
?order by case when no='4' then 1?
???else 1+rand()?
??end?

iii.?
--查詢要求所有行隨機排序

--解決方案?
select * from ai?
?order by newid()?


iiii?
--有一表ab有列i,其中數據如下:?
i varchar(10)?
a1?
a10?
a101?
a5?
p4?
p41?
p5?


--現在要求列i中數據先按字母排序,再按數字排序?
--效果如下:?
a1?
a5?
a10?
a101?
p4?
p5?
p41?

--解決方案?
select * from ab?
?order by left(i,1),convert(int,substring(i,2,8000))

總結

以上是生活随笔為你收集整理的表中记录查询排序(设置排序规则)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。