(1-7-3)数据库的基本查询

发布于:2025-07-19 ⋅ 阅读:(13) ⋅ 点赞:(0)

目录

1. 数据库的基本查询

1.1 简单的记录查询

1.2 使用列别名

2.  数据分页查询

(1)查询前五行数据

(2)查询 11 ~  15 行数据

3. 结果集排序

3.1 单关键字排序

(1)升序排列

(2)降序排列

(3)按照名字降序排列

3.2 多关键字排序

(1)按照 薪资降序 + 入职日期升序  查询

(2)部门升序 + 薪资降序 查询

3.3 排序 + 分页

(1)薪资 默认升序排列 1 ~ 5 行数据

4. 去除重复记录

4.1 去除 job 的相同行数据

4.2 去除 job与其他关键字联合 的相同行数据

5. 条件查询

(1)查询部门编号为20  且薪资大于1500 的员工信息

(2)查询部门编号为20 或 30, 且薪资大于 1300 的员工信息

(3)查询部门编号为 10 或 30,且年收入大于17000, 且工龄超过20年

(4)查询部门编号为 10、20、30, 且 工作不是(salesman 或 president),且入职年月早于“1995-01-01”

(5) 查询奖金(comm)为空 的员工信息(带小节)

(6)查询奖金(comm)不为空 的员工信息

(7)查询奖金(comm)不为空 薪资在1500与2500之前 的员工信息

(8) 查询奖金(comm)不为空 且薪资在1500与3000之前 姓名中包含A 的员工信息

(9)查询姓名为“__RD”的员工

(10)通过正则表达式查询  长度为2~4个字的汉字字符

(11) 逻辑运算符

(12)查询 员工编号不为10、20 的员工信息

(13)查询 (员工编号不为10、20) 异或 (薪资大于1800) 的员工信息 

(14) 按位运算符

(15) where 子句的注意事项

6. 各种子句的执行顺序


前置知识总结:


示例数据库文件:

通过网盘分享的文件:demo.sql
链接: https://pan.baidu.com/s/1iUKSMScC1PXtbOFFaKm-vQ 提取码: i5hb 复制这段内容后打开百度网盘手机App,操作更方便哦




1. 数据库的基本查询

1.1 简单的记录查询

select * from t_emp;

select empno, ename, sal, deptno from t_emp;

1.2 使用列别名

select empno, ename, sal*14 as "year-income" from t_emp;

2.  数据分页查询

(1)查询前五行数据

方式一:

# 查询 1 ~ 5 行数据
select empno, ename, sal, deptno from t_emp limit 0,5;

 方式二:

# 查询 1 ~ 5 行数据
select empno, ename, sal, deptno from t_emp limit 5;

(2)查询 11 ~  15 行数据

# 查询 11 ~ 15 行数据
select empno, ename, sal, deptno from t_emp limit 10,5;

3. 结果集排序

3.1 单关键字排序

(1)升序排列

select empno, ename, sal from t_emp order by sal;

select empno, ename, sal from t_emp order by sal asc;
(2)降序排列

# 按照薪资 降序排列
select empno, ename, sal from t_emp order by sal desc;
(3)按照名字降序排列

3.2 多关键字排序

(1)按照 薪资降序 + 入职日期升序  查询

# 按照 薪资降序 + 入职日期升序  查询
select 
  empno, ename, sal, hiredate 
from t_emp 
order by sal desc, hiredate asc;
(2)部门升序 + 薪资降序 查询

# 按照 部门升序 + 薪资降序 查询
select 
  empno, ename, sal, deptno
from t_emp 
order by deptno asc, sal desc;

3.3 排序 + 分页

(1)薪资 默认升序排列 1 ~ 5 行数据

# 按照薪资 默认升序排列 1 ~ 5 行数据
select empno, ename, sal from t_emp order by sal limit 5;

4. 去除重复记录

4.1 去除 job 的相同行数据

select job from t_emp;
# 去除 job 的相同行数据
select distinct job from t_emp;

4.2 去除 job与其他关键字联合 的相同行数据

select job, sal from t_emp;
# 去除 job与其他关键字联合 的相同行数据
select distinct job, sal from t_emp;

5. 条件查询

(1)查询部门编号为20  且薪资大于1500 的员工信息

# 查询部门编号为20  且薪资大于1500 的员工信息
select empno, ename, sal, job 
  from t_emp
where deptno = 20 and sal >= 1500;

(2)查询部门编号为20 或 30, 且薪资大于 1300 的员工信息

# 查询部门编号为20 或 30, 且薪资大于 1300 的员工信息
select empno, ename, sal, job
  from t_emp 
where (deptno = 20 or deptno = 30) and sal >= 1300;

(3)查询部门编号为 10 或 30,且年收入大于17000, 且工龄超过20年

# 查询部门编号为 10 或 30,且年收入大于17000, 且工龄超过20年
select empno, ename, sal, job
  from t_emp 
where (deptno = 10 or deptno = 30) 
and (sal + ifnull(comm, 0)) * 12 >= 17000
and datediff(now(), hiredate)/365 >= 20;

(4)查询部门编号为 10、20、30, 且 工作不是(salesman 或 president),且入职年月早于“1995-01-01”

# 查询部门编号为 10、20、30, 且 工作不是(salesman 或 president),且入职年月早于“1995-01-01”
select *
 from t_emp
where deptno in(10, 20, 30) 
and (job != "SALESMAN" and job != "PRESIDENT" )
and hiredate < "1982-01-01";

(5) 查询奖金(comm)为空 的员工信息(带小节)

# 查询奖金(comm)为空 的员工信息
select *
  from t_emp 
where comm is null;

(6)查询奖金(comm)不为空 的员工信息

# 查询奖金(comm)不为空 的员工信息
select *
  from t_emp 
where comm is not null;

(7)查询奖金(comm)不为空 薪资在1500与2500之前 的员工信息

# 查询奖金(comm)不为空 薪资在1500与2500之前 的员工信息
select *
  from t_emp 
where comm is not null
and sal between 1500 and 2500;

(8) 查询奖金(comm)不为空 且薪资在1500与3000之前 姓名中包含A 的员工信息

# 查询奖金(comm)不为空 且薪资在1500与3000之前 姓名中包含A 的员工信息
select *
  from t_emp 
where comm is not null
and sal between 1500 and 3000
and ename like "%A%";

(9)查询姓名为“__RD”的员工

# 查询姓名为“__RD”的员工
select *
  from t_emp 
where ename like "__RD"

(10)通过正则表达式查询  长度为2~4个字的汉字字符

# 通过正则表达式查询  长度为2~4个字的汉字字符
select *
  from t_emp 
where ename 
regexp "^[\\u4e00-\\u9fa5]{2,4}$";

(11) 逻辑运算符

(12)查询 员工编号不为10、20 的员工信息

# 查询 员工编号不为10、20 的员工信息
select *
from t_emp 
where not deptno in(10, 20);

(13)查询 (员工编号不为10、20) 异或 (薪资大于1800) 的员工信息 

# 查询 (员工编号不为10、20) 异或 (薪资大于1800) 的员工信息  
select empno, ename, deptno, sal
from t_emp
where not deptno in(10, 20) 
xor sal >= 1800;
(14) 按位运算符

14.1 3&7

14.2 3|7

14.3 ~10

14.4 3^7

14.5  10 << 1

15.6 10>>1

(15) where 子句的注意事项

6. 各种子句的执行顺序


网站公告

今日签到

点亮在社区的每一天
去签到