久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

sql server中order by部分使用方式

瀏覽:5日期:2023-11-01 19:19:11

order by常用的使用方式我就不提了

項目的需求千變萬化 讓我們看看下面幾個怪排序需求

--先創建一個表 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,并且每組再按照id降序排列--利用函數case select * from ai order by case when no='4' then 1 when no='1' then 2 when no='2' then 3 end,id desc

--解決方案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))

標簽: Sql Server 數據庫
主站蜘蛛池模板: 黑水县| 宁津县| 平果县| 福泉市| 高雄县| 包头市| 景宁| 客服| 犍为县| 儋州市| 明光市| 萨迦县| 自贡市| 琼海市| 安溪县| 缙云县| 云林县| 濮阳市| 丘北县| 舒兰市| 咸阳市| 会宁县| 二连浩特市| 晋中市| 龙口市| 磴口县| 东源县| 翁牛特旗| 渭南市| 义乌市| 石嘴山市| 宜城市| 香港| 枝江市| 泾阳县| 华阴市| 林甸县| 齐齐哈尔市| 樟树市| 张家川| 丹寨县|