(数据库)06_过滤和排序数据

一、过滤查询

•使用WHERE 子句,将不满足条件的行过滤掉。
•WHERE 子句紧随 FROM 子句。

--查询id为90的部门的所有员工
--where  后边所跟的就对数据的过滤条件
select * from employees where department_id =90;

二、对值为字符串的进行过滤(字符和日期)

①字符和日期要包含在单引号中。
②字符大小写敏感,日期格式敏感。
③默认的日期格式是 DD-MON月-RR。

--查询last_name为Whalen的员工
-- 在过滤条件中 字符串必须使用单引号引起来 同时对值的过滤 是区分大小写
-- = 表示严格匹配
select * from employees where last_name = 'Whalen';
--查询入职时间为7-6月-1994
select * from employees where hire_date='17-JUN-2003';

三、比较运算

在这里插入图片描述

--查询薪资小于等于3000的雇员信息
select * from employees where salary <= 3000;
--查询工龄大于Steven的雇员信息
select * from employees where hire_date<'17-JUN-2003';
--查询不是90部门的所有的雇员信息
select * from employees where department_id <>90;
select * from employees where department_id !=90;

1.其它比较运算

在这里插入图片描述

1.between

使用 BETWEEN 运算来显示在一个区间内的值

--工资在2500到3500之间的雇员信息 包含边界值
select *from employees where salary between 2500 and 3500;
select * from employees where salary >= 2500 and salary <= 3500;

2.in

-- IN  查询雇员编号为 115 116 117 的雇员信息
select * from employees where employee_id = 115 or employee_id=116 or employee_id=117;
select * from employees where employee_id in(115,116,117);

3.like

  1. 使用 LIKE 运算选择类似的值
  2. 选择条件可以包含字符或数字:
  3. % 代表零个或多个字符(任意个字符)
  4. _ 代表一个字符
--like  查询雇员名以字母A开头的雇员信息 需要结合% 、_两个通配符使用
select * from employees where first_name like 'A%';
--查询雇员名中的第二个字母为a的雇员信息
select * from employees where first_name like '_a%';
--查询雇员名字中包含字母a的雇员信息
select * from employees where first_name like '%a%';

select * from employees where first_name like '%A%';等同与
select * from employees where first_name like 'A%';

4.null

使用 IS (NOT) NULL 判断空值。

--IS NULL 查询没有奖金的雇员信息
select * from employees where commission_pct is null;
--IS NULL 查询有奖金的雇员信息
select * from employees where commission_pct is not null;

5.ESCAPE

回避特殊符号的:使用转义符。例如:将[%]转为[%]、[_]转为[_],然后再加上[ESCAPE ‘\’] 即可。

SELECT job_id
FROM   jobs
WHERE  job_id LIKE 'IT\_%' escape '\';

四、逻辑运算

在这里插入图片描述
实例见上方

优先级:

在这里插入图片描述
可以使用括号改变优先级顺序

五、ORDER BY子句

使用 ORDER BY 子句排序:

  1. ASC(ascend): 升序
  2. DESC(descend): 降序

ORDER BY 子句在SELECT语句的结尾

1.升序、降序排序

--ORDER BY 
--查询所有的雇员信息 并根据薪资进行排序  当不指明排序规则时 默认时升序
select * from employees order by salary;
----查询所有的雇员信息 并根据薪资进行排序 从大到小 降序
select * from employees order by salary desc;
--查询所有的雇员信息 根据first_name 进行降序排序 
--对于值为字符串的字段的排序 是根据字典顺序
select * from employees order by first_name ;
--查询所有的100部门的员工信息 并根据入职时间进行升序显示
select * from employees where department_id =100 order by hire_Date;

2.别名排序:

-- 计算所有的雇员的年薪(yearSalary)包含奖金 并按照年薪进行排序
select  first_name ,last_name ,salary ,salary * (1 + NVL(commission_pct,0))*12 YEARSALARY  
from employees 
ORDER BY YEARSALARY;

3.多字段排序:

在order by 后边可以跟多个字段 当第一个字段的值相同时 则根据第二个字段进行排序 并且可以分别为每个字段指定排序规则

--查询所有的雇员信息 并按照入职时间排序 如果入职时间相同 则按照薪资从高到底排序
select * from employees order by hire_date desc ,salary asc;

可以使用不在SELECT 列表中的列排序。

总结 :

select 确定要查询的字段
from  确定数据源
where 对所有的数据按找给定的条件进行过滤:
	①使用比较运算
	②使用 BETWEEN AND, IN, LIKE和 NULL运算
	③使用逻辑运算符 AND, OR和NOT
order by 对过滤之后的数据进行按照指定的字段排序

随堂练习链接:

随堂练习

发布了67 篇原创文章 · 获赞 6 · 访问量 1935

猜你喜欢

转载自blog.csdn.net/weixin_45801537/article/details/104153802