SqlServer 基础知识 数据检索、查询排序语句
2022-11-12 09:49:24
内容摘要
这篇文章主要为大家详细介绍了SqlServer 基础知识 数据检索、查询排序语句,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
代码如下:
文章正文
这篇文章主要为大家详细介绍了SqlServer 基础知识 数据检索、查询排序语句,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <code> --执行顺序 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 --从表中随机返回N条记录 newid() --order by 字句中指定数字常量时,是要求根据select列表中相应位置的列排序 --order by 字句中用函数时,则按函数在没一行计算结果排序 select top 5 ename from emp order by newid() --找空值is null select * from emp where comm is null --将空值转换为实际值 --解释:返回其参数中第一个非空表达式 --coalesce 联合,合并,结合.英音:[,kəuə 'les]美音:[,koə' lɛs] select coalesce(comm, 1),empNo from emp --按模式搜索 --返回匹配特定子串或模式的行 select ename,job from emp where deptno in(10,20) --按子串排序 按照职位字段的 最后两个字符排序 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 </code> |
【图片暂缺】
【图片暂缺】
注:关于SqlServer 基础知识 数据检索、查询排序语句的内容就先介绍到这里,更多相关文章的可以留意
代码注释