oracle数据库学习笔记(三)

1.排序问题
默认排序规则:按照物理存储的顺序。
指定排序规则:order by关键字
位置:放到select查询语句的最后面。
语法:select..from...order by 字段 排序规则;
字段用来声明根据哪个字段进行排序。
排序规则:
1)升序排列 从小到大 asc
2)降序排列 从大到小 desc
例如:查询所有员工的id、salary
要求按照工资的降序排列?
select id,salary
from s_emp
order by salary desc;

指定多个排序规则:
语法:select..
from...
order by 字段1 排序规则1,
字段2 排序规则2......;
例如:查询所有员工的id、salary
按照工资的降序和id的升序排列?
select id,salary
from s_emp
order by salary desc,id asc;

空值的处理:
commission_pct --提成
在排序时,空值视为无限大。
在降序排列中,空值排在最前面。
在升序排列中,空值排在最后面。

2.限定性查询/条件查询
在查询时指定查询条件,
用来筛除掉一部分不需要的数据。
关键字:where
位置:放到from子句后面。
select ... from ... where .... order by ...;
语法:select..from..where 判断条件;

1)等值判断和不等值判断
字段值等于或不等于某个特定的值。
例如:
查询41号部门的员工信息?
字段:dept_id
select id,last_name,dept_id
from s_emp
where dept_id = 41;

如何表示不等于
三种写法:
写法一:!=
写法二:^=
写法三:<>

空值的处理:
为空:is null
不为空:is not null
例如:
查询所有不拿提成的员工信息?
select id,last_name,salary
from s_emp
where commission_pct is null;

查询所有拿提成的员工信息?
select ...
from ..
where commission_pct is not null;

2)范围判断
大于 >
小于 <
大于等于 >=
小于等于 <=

例如:
查询所有工资高于1100元的员工信息?
select id,last_name,salary
from s_emp
where salary > 1100;

查询所有工资不低于1100元的员工信息?
select id,last_name,salary
from s_emp
where salary >= 1100;

3)条件并列
a)逻辑与 and
使用and连接的所有条件必须同时满足
则该条数据才会被查询出来。
b)逻辑或 or
使用or连接的所有条件只需要满足其中之一,
这条数据就会被查询出来。

例如:
查询41号部门工资大于1400元的员工信息?
select id,last_name,dept_id
from s_emp
where dept_id = 41 and salary>1400;

查询所有41、42号部门的员工信息?
select id,last_name,dept_id
from s_emp
where dept_id = 41 or dept_id = 42;

查询工资高于1400元的,并且工作在41或42部门的
员工信息?
select id,last_name,salary
from s_emp
where salary>1400
and (dept_id = 41 or dept_id = 42);

优先级问题:
and的优先级要高于or。

扫描二维码关注公众号,回复: 8430980 查看本文章

4)逻辑比较符
用来简化条件并列
between 从给定的最小值和最大值范围内取值
语法:where 字段 between 较小值 and 较大值;
where salary between 1000 and 1500;
等同于:
where salary >= 1000 and salary <=1500;
注意:一定要把较小值写在and前,
把较大值写在and后。

in 从给定几个固定值中取一个
语法:where 字段 in(值1,值2,值3.....);

例如:
查询1、3、5、7、9号员工的基本信息?
select id,last_name
from s_emp
where id = 1 or id = 3 or id = 5
or id = 7 or id = 9;

select id,last_name
from s_emp
where id in(1,3,5,7,9);

查询工资不在1000-1500范围内的员工信息?
select id,last_name
from s_emp
where salary not between 1000 and 1500;

查询41、42号部门以外的员工信息?
select ...
from ...
where dept_id!=41 and dept_id!=42;

select ..
from ...
where dept_id not in(41,42);

3.模糊查询/关键字查询
语法:where 字段 like 模糊值;

通配符:
1)% 百分号代表任意数量任意字符
可以没有 可以有一个 可以有多个
2)_ 下划线代表一个任意字符。--占位符
必须有且只有一个字符。

例如:查询所有last_name中包含'n'的员工信息?
select id,last_name
from s_emp
where last_name like '%n%';

转义字符:
先插入一条数据:
insert into s_emp(id,last_name)
values(999,'_briup');
commit;

例如:
查询所有名字以'_'开头的员工信息?
select id,last_name
from s_emp
where last_name like '/_%' escape '/';

转义的步骤:
1)在要转义的字符前面加一个标识。
标识可以是任意字符。
like 'a_%';
2)使用escape声明哪个字符是标识字符
like 'a_%' escape 'a';

 

 

猜你喜欢

转载自www.cnblogs.com/DennySmith/p/12153549.html
今日推荐