Oracle条件与排序查询

Oracle条件与排序查询

select *|字段名
from 表名
where 条件
order by 字段名  asc[默认值]|desc
-- 查询每月能得到奖金的雇员: 
--空值的处理:要使用关键字: is null,  is not null
select * from emp where comm is not null and comm >0;

--我们直接使用大于号
select * from emp where comm >0;


--案例:查询工资大于 1500 并且有奖金领取的雇员
select * from emp where sal > 1500 and comm > 0;


--查询工资大于 1500 或者有奖金的雇员
select * from emp where sal > 1500 or comm > 0 ;

--查询工资不大于 1500 和没有奖金的人
select * from emp where sal <= 1500 and (comm is null or comm = 0);

范围限制

between and 不仅可以使用在数值之间,也可以用在日期的区间

在 oracle 中的查询条件中查询条件的"值"是区分大小写的,其他不区分

IN 关键字
语法: 列名 IN (值 1,值 2,....)
列名 NOT IN (值 1, 值 2,...)
其中的值不仅可以是数值类型也可以是字符串
-- 查询工资在 1500 到 3000之间,包含边界值
select * from emp where sal between 1500 and 3000;


--查询 1981-1-1 到 1981-12-31 号入职的雇员
select * from emp where hiredate between to_date('1981-1-1','yyyy-mm-dd') and to_date('1981-12-31','yyyy-mm-dd');

--查询雇员名字叫 smith 的雇员:Oracle区分大小写
select * from emp where ename = 'SMITH';

模糊查询

sql 中使用 LIKE 语句完成。
在 LIKE 中主要使用以下两种通配符
“%”:可以匹配任意长度的内容
“_”:可以匹配一个长度的内容
--模糊查询
--查找名字中有 M 员工
select * from emp where ename like '%M%'; 

--查询员工姓名第二个字母是  M 的
select * from emp where ename like '_M%';

使用 ORDER BY 对查询结果进行排序

语法:SELECT * |列名 FROM 表名 {WEHRE 查询条件} ORDER BY 列名 1 ASC|DESC,列名 2...ASC|DESC

分析:ORDER BY 列名 默认的排序规则是升序排列,可以不指定 ASC,如果按着降序排列必须指定DESC
--查询员工的工资,按照由到小排序
select  * from emp order by sal desc;

排序中的空值问题

当排序时有可能存在 null 时就会产生问题,

我们可以用 nulls first , nulls last 来指定 null 值显示的位置。
-- 对员工的奖金排序:处理空值: nulls last【把空值放到最后】 |  nulls first【把空值放到最前】
select * from emp  order by comm desc nulls last;

猜你喜欢

转载自blog.csdn.net/h294590501/article/details/81591882
今日推荐