SqlServer 基礎(chǔ)知識(shí) 數(shù)據(jù)檢索、查詢排序語(yǔ)句
發(fā)布日期:2022-01-23 09:42 | 文章來(lái)源:站長(zhǎng)之家
復(fù)制代碼 代碼如下:
--執(zhí)行順序 From Where Select
select * from
(select sal as salary,comm as commission from emp ) x where salary<5000
--得出 Name Work as a Job
select ename +' Work as a'+job as msg from emp where deptno=10
--如果員工工資小于2000返回UnderPaid 大于等于4k 返回OverPaid 之間返回OK
select ename,sal,
case when sal<2000 then 'UnderPaid'
when sal>=4000 then 'OverPaid'
else
'OK'
end
from emp
--從表中隨機(jī)返回N條記錄 newid()
--order by 字句中指定數(shù)字常量時(shí),是要求根據(jù)select列表中相應(yīng)位置的列排序
--order by 字句中用函數(shù)時(shí),則按函數(shù)在沒(méi)一行計(jì)算結(jié)果排序
select top 5 ename from emp order by newid()
--找空值is null
select * from emp where comm is null
--將空值轉(zhuǎn)換為實(shí)際值
--解釋:返回其參數(shù)中第一個(gè)非空表達(dá)式
--coalesce 聯(lián)合,合并,結(jié)合.英音:[,kəuə'les]美音:[,koə'lɛs]
select coalesce(comm, 1),empNo from emp
--按模式搜索
--返回匹配特定子串或模式的行
select ename,job
from emp
where deptno in(10,20)
--按子串排序 按照職位字段的 最后兩個(gè)字符排序
select ename, job from emp order by substring(job,len(job)-2,2)
--select top 2 len(job)-2 from emp
--select top 2 job from emp
--☆☆☆☆☆處理排序空值☆☆☆☆☆ [只能是大于0]
select ename ,sal,comm
from emp
order by 1 desc
-- 以降序或升序方式排序非空值,將空值放到最后,可以用case
select ename,sal,comm from
(
select ename ,sal,comm ,
case when comm is null then 0 else 1 end as A
from emp
) x
order by A desc ,comm desc


版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信