数据库查询-DQL
DQL

语法

基本查询

例如:
(源代码)
java
-- DQL:基本查询
-- 1.查询指定字段 name,entrydate 并返回
select name,entrydate from tb_emp;
-- 2. 查询返回所有字段
select id, username, password, name, gender, image, job, entrydate, create_time, update_time from tb_emp;
-- 3.查询所有员工的 name,entrydate 并起别名(姓名,入职日期)
select name as '姓名' , entrydate as '入职日期' from tb_emp;
-- 4.查询已有的员工关联了哪几种职位
select distinct job from tb_emp;
条件查询

例如:
(源代码)
java
-- =================== DQL: 条件查询 ======================
-- 1. 查询 姓名 为 杨逍 的员工
select * from tb_emp where name = '杨逍';
-- 2. 查询 id小于等于5 的员工信息
select * from tb_emp where id <= 5;
-- 3. 查询 没有分配职位 的员工信息
select * from tb_emp where job is null ;
-- 4. 查询 有职位 的员工信息
select * from tb_emp where job is not null ;
-- 5. 查询 密码不等于 '123456' 的员工信息
select * from tb_emp where password != '123456';
select * from tb_emp where password <> '123456';
-- 6. 查询 入职日期 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之间的员工信息
select * from tb_emp where entrydate >= '2000-01-01' and entrydate <= '2010-01-01';
select * from tb_emp where entrydate between '2000-01-01' and '2010-01-01';
-- 7. 查询 入职时间 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之间 且 性别为女 的员工信息
select * from tb_emp where entrydate between '2000-01-01' and '2010-01-01' && gender = 2;
-- 8. 查询 职位是 2 (讲师), 3 (学工主管), 4 (教研主管) 的员工信息
select * from tb_emp where job = 2 or job = 3 || job = 4;
select * from tb_emp where job in (2,3,4);
-- 9. 查询 姓名 为两个字的员工信息
select * from tb_emp where name like '__';
-- 10. 查询 姓 '张' 的员工信息
select * from tb_emp where name like '张%';
聚合函数

例如:
(源代码)
java
-- =================== DQL: 分组查询 ======================
-- 聚合函数
-- 1. 统计该企业员工数量
select count(*) from tb_emp;
-- 2. 统计该企业员工 ID 的平均值
select avg(id) from tb_emp;
-- 3. 统计该企业最早入职的员工
select min(entrydate) from tb_emp;
-- 4. 统计该企业最迟入职的员工
select max(entrydate) from tb_emp;
-- 5. 统计该企业员工的 ID 之和
select sum(id) from tb_emp;
分组查询

例如:
(源代码)
java
-- 分组
-- 1. 根据性别分组 , 统计男性和女性员工的数量
select gender,count(*) from tb_emp group by gender;
-- 3. 先查询入职时间在 '2015-01-01' (包含) 以前的员工 , 并对结果根据职位分组 , 获取员工数量大于等于2的职位
select job,count(*) from tb_emp where entrydate <= '2015-01-01' group by job having count(*) >= 2;
排序查询

例如:
(源代码)
java
-- =================== 排序查询 ======================
-- 1. 根据入职时间, 对员工进行升序排序
select * from tb_emp order by entrydate asc;
select * from tb_emp order by entrydate;
-- 2. 根据入职时间, 对员工进行降序排序
select * from tb_emp order by entrydate desc;
-- 3. 根据 入职时间 对公司的员工进行 升序排序 , 入职时间相同 , 再按照 更新时间 进行降序排序
select * from tb_emp order by entrydate , update_time desc ;
分页查询

例如:
(源代码)
java
-- =================== 分页查询 ======================
-- 1. 从起始索引0开始查询员工数据, 每页展示5条记录
select * from tb_emp limit 0,5;
-- 2. 查询 第1页 员工数据, 每页展示5条记录
select * from tb_emp limit 5;
-- 3. 查询 第2页 员工数据, 每页展示5条记录
select * from tb_emp limit 5,5;
-- 4. 查询 第3页 员工数据, 每页展示5条记录
select * from tb_emp limit 10,5;
案例
案例1

例如:
(源代码)
java
-- 分组
-- 1. 根据性别分组 , 统计男性和女性员工的数量 -- count
select gender,count(*) from tb_emp group by gender ;
-- 2. 先查询入职时间在 '2015-01-01' (包含) 以前的员工 , 并对结果根据职位分组 , 获取员工数量大于等于2的职位 -- count
select job,count(*) from tb_emp where entrydate <= '2015-01-01' group by job having count(*) >= 2;
(运行结果截图)


案例2

【员工性别统计】
(源代码)
java
-- 男性与女性员工的人数统计 (1 : 男性员工 , 2 : 女性员工)
-- 函数: if(条件表达式 , t , f)
select if(gender = 1, '男性', '女性') as '性别', count(*) as '人数'
from tb_emp
group by gender;
(运行结果截图)

【员工职位统计】
(源代码)
java
-- 员工职位信息 -- count '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管'
-- 函数: case when ... then ... when ... then ... else ... end
-- 函数: case ... when ... then ... when ... then ... else ... end
select (case job
when 1 then '班主任'
when 2 then '讲师'
when 3 then '学工主管'
when 4 then '教研主管'
else '没有分配职位' end) as '职位',
count(*) as '人数'
from tb_emp
group by job;
(运行结果截图)

小结

多表设计
一对多

例如:

(源代码)
java
-- 员工管理(带约束)
create table tb_emp
(
id int unsigned primary key auto_increment comment 'ID',
username varchar(20) not null unique comment '用户名',
password varchar(32) default '123456' comment '密码',
name varchar(10) not null comment '姓名',
gender tinyint unsigned not null comment '性别, 说明: 1 男, 2 女',
image varchar(300) comment '图像',
job tinyint unsigned comment '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管',
entrydate date comment '入职时间',
dept_id int unsigned comment '归属部门ID',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间'
) comment '员工表';
-- 部门表
create table tb_dept
(
id int unsigned primary key auto_increment comment 'ID',
name varchar(10) not null unique comment '部门名称',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间'
);
外键约束


例如:
(源代码)
java
alter table tb_emp
add constraint tb_emp_fk_dept_id foreign key (dept_id) references tb_dept (id);
一对一

例如:
(源代码)
java
-- ===========================================一对一=====================================
create table tb_user
(
id int unsigned primary key auto_increment comment 'ID',
name varchar(10) not null comment '姓名',
gender tinyint unsigned not null comment '性别, 1 男 2 女',
phone char(11) comment '手机号',
degree varchar(10) comment '学历'
) comment '用户信息表';
insert into tb_user
values (1, '白眉鹰王', 1, '18812340001', '初中'),
(2, '青翼蝠王', 1, '18812340002', '大专'),
(3, '金毛狮王', 1, '18812340003', '初中'),
(4, '紫衫龙王', 2, '18812340004', '硕士');
create table tb_user_card
(
id int unsigned primary key auto_increment comment 'ID',
nationality varchar(10) not null comment '民族',
birthday date not null comment '生日',
idcard char(18) not null comment '身份证号',
issued varchar(20) not null comment '签发机关',
expire_begin date not null comment '有效期限-开始',
expire_end date comment '有效期限-结束',
user_id int unsigned not null unique comment '用户ID',
constraint fk_user_id foreign key (user_id) references tb_user(id)
) comment '用户信息表';
insert into tb_user_card
values (1, '汉', '1960-11-06', '100000100000100001', '朝阳区公安局', '2000-06-10', null, 1),
(2, '汉', '1971-11-06', '100000100000100002', '静安区公安局', '2005-06-10', '2025-06-10', 2),
(3, '汉', '1963-11-06', '100000100000100003', '昌平区公安局', '2006-06-10', null, 3),
(4, '回', '1980-11-06', '100000100000100004', '海淀区公安局', '2008-06-10', '2028-06-10', 4);
(运行结果截图)

多对多

例如:
(源代码)
java
-- ======================================多对多=============================
create table tb_student
(
id int auto_increment primary key comment '主键ID',
name varchar(10) comment '姓名',
no varchar(10) comment '学号'
) comment '学生表';
insert into tb_student(name, no)
values ('黛绮丝', '2000100101'),
('谢逊', '2000100102'),
('殷天正', '2000100103'),
('韦一笑', '2000100104');
create table tb_course
(
id int auto_increment primary key comment '主键ID',
name varchar(10) comment '课程名称'
) comment '课程表';
insert into tb_course (name)
values ('Java'),
('PHP'),
('MySQL'),
('Hadoop');
create table tb_student_course
(
id int auto_increment comment '主键' primary key,
student_id int not null comment '学生ID',
course_id int not null comment '课程ID',
constraint fk_student_id foreign key (student_id) references tb_student(id),
constraint fk_course_id foreign key (course_id) references tb_course(id)
) comment '学生课程中间表';
insert into tb_student_course(student_id, course_id)
values (1, 1),
(1, 2),
(1, 3),
(2, 2),
(2, 3),
(3, 4);
(运行结果截图)

案例

【关系分析】

【表结构】
分类表

java
-- 瑞吉点餐页面原型 , 设计表结构
-- 分类表
create table category
(
id int unsigned primary key auto_increment comment '主键ID',
name varchar(20) not null unique comment '分类名称',
type tinyint unsigned not null comment '分类类型 1:菜品分类 2:套餐分类',
sort tinyint unsigned not null comment '排序',
status tinyint unsigned default '0' comment '状态 1:启用 0:停用',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间'
) comment '分类表';
菜品表

java
-- 菜品表
create table dish
(
id int unsigned primary key auto_increment comment '主键ID',
name varchar(20) not null unique comment '菜品名称',
category_id int unsigned not null comment '菜品分类',
price decimal(8,2) not null comment '价格',
image varchar(300) not null comment '图片',
decription varchar(200) comment '菜品描述',
status tinyint unsigned default '0' comment '状态 1:启用 0:停用',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间'
) comment '菜品表';
套餐表

java
-- 套餐表
create table setmeal
(
id int unsigned primary key auto_increment comment '主键ID',
name varchar(20) not null unique comment '套餐名称',
category_id int unsigned not null comment '套餐分类',
price decimal(8,2) not null comment '价格',
image varchar(300) not null comment '图片',
decription varchar(200) comment '套餐描述',
status tinyint unsigned default '0' comment '状态 1:启用 0:停用',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间'
) comment '套餐表';
套餐菜品关联表
java
-- 套餐菜品关联表
create table setmeal_dish
(
id int unsigned primary key auto_increment comment '主键ID',
setmeal_id int unsigned not null comment '套餐ID',
dish_id int unsigned not null comment '菜品ID',
copies int unsigned not null comment '份数',
constraint fk_setmeal_id foreign key (setmeal_id) references setmeal(id),
constraint fk_dish_id foreign key (dish_id) references dish(id)
) comment '菜品表';
小结
