数据库设计------DQL
介绍
DQL英文全称是Data Query Language(数据查询语言),用来查询数据库表中的记录。
查询关键字:SELECT
查询操作是所有SQL语句当中最为常见,也是最为重要的操作。在一个正常的业务系统中,查询操作的使用频次是要远高于增删改操作的。当我们打开某个网站或APP所看到的展示信息,都是通过从数据库中查询得到的,而在这个查询过程中,还会涉及到条件、排序、分页等操作。
语法
DQL查询语句,语法结构如下:

基本查询
查询多个字段
select 字段1, 字段2, 字段3 from 表名;
案例1:查询指定字段 name,entrydate并返回
select name,entrydate from tb_emp;

查询所有字段(通配符)
select * from 表名;
案例2:查询返回所有字段
select * from tb_emp;
`*`号代表查询所有字段,在实际开发中尽量少用(不直观)

设置别名
select 字段1 [ as 别名1 ] , 字段2 [ as 别名2 ] from 表名;
案例3:查询所有员工的 name,entrydate,并起别名(姓名、入职日期)
使用''或""包含
select name AS '姓 名', entrydate AS '入职日期' from tb_emp;
select name AS "姓名", entrydate AS "入职日期" from tb_emp;

去除重复记录
select distinct 字段列表 from 表名;
案例4:查询已有的员工关联了哪几种职位(不要重复)
select distinct job from tb_emp;

条件查询
语法:
select 字段列表 from 表名 where 条件列表 ; -- 条件列表:意味着可以有多个条件
学习条件查询就是学习条件的构建方式,而在SQL语句当中构造条件的运算符分为两类:
- 比较运算符
- 逻辑运算符

案例1:查询 姓名 为 杨逍 的员工

案例2:查询 id小于等于5 的员工信息
select id, username, password, name, gender, image, job, entrydate, create_time, update_time from tb_emp where id <=5;

案例3:查询 没有分配职位 的员工信息
select id, username, password, name, gender, image, job, entrydate, create_time, update_time from tb_emp where job is null ;

注意:查询为NULL的数据时,不能使用 `= null`

案例4:查询 有职位 的员工信息

案例5:查询 密码不等于 '123456' 的员工信息

案例6:查询 入职日期 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之间的员工信息

案例7:查询 入职时间 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之间 且 性别为女 的员工信息

案例8:查询 职位是 2 (讲师), 3 (学工主管), 4 (教研主管) 的员工信息

案例9:查询 姓名 为两个字的员工信息

案例10:查询 姓 '张' 的员工信息
