查询多个字段
select 字段1,字段2... from 表名;
查询所有字段(通配符)
select * from 表名
设置别名
select 字段1 as 别名1,字段2 as 别名2 from 表名;
去除重复记录
select distinct 字段列表 from 表名
练习
# 1.查询字段 name,entrydate
select name,entrydate from tb_emp;
# 2.查询返回所有字段
select * from tb_emp;
# 3.查询所有员工的name,entrydate,并起别名(姓名、入职日期)
select name as 姓名,entrydate as 入职日期 from tb_emp;
# 4.查询已有的员工关联了哪几种职位
select distinct job from tb_emp;
通配符代表查询所有字段,在实际开发中尽量少用(不直观、影响效率)
语法
select 字段列表 from 表名 where 条件列表
运算符
# 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 between '2000-01-01' and '2010-01-01';
select * from tb_emp where entrydate >= '2000-01-01' and entrydate <= '2010-01-01';
#7. 查询入职日期在 '2000-01-01'(包含) 到 '2010-01-01'(包含)之间且性别为女的员工信息
select * from tb_emp where gender = 2 and entrydate between '2000-01-01' and '2010-01-01';
#8.查询职位是2,3,4的员工信息
select * from tb_emp where job in (2,3,4);
select * from tb_emp where job = 2 or job = 3 or job = 4;
#9.查询姓名为两个字的员工
select * from tb_emp where name like '__';
#10.查询姓'张'的员工信息
select * from tb_emp where name like '张%';
聚合函数
语法
select 聚合函数(字段列别) from 表名;
函数
#1.统计该企业员工数量
#A count(字段)
select count(id) from tb_emp;
#B.count (常量)
select count(2) from tb_emp;
#C.count(*)
select count(*) from tb_emp;
#2.统计该企业最早如期的员工
select min(entrydate) from tb_emp;
#3.统计该企业最迟入职的员工
select max(entrydate) from tb_emp;
#4.统计该企业员工ID平均值
select avg(id) from tb_emp;
#5.统计员工ID之和
select sum(id) from tb_emp;
【注意事项】
分组查询
select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组后过滤条件];
where 与 having区别
练习
#1.根据性别分组,统计男性和女性员工的数量
select gender,count(gender) from tb_emp group by gender;
#2.先查询入职时间在'2015-01-01'(包含)以前的员工,并对结果根据职位分组,获取员工数量大于等于2的职位
select job,count(*) from tb_emp where entrydate <= '2015-01-01' group by job having count(*)>=2;
排序
select 字段列表 from 表名 [where 条件] [group by 分组字段] order by 字段1 排序方式1,字段2 排序方式2...;
排序方式
练习
#1.根据入职时间,对员工进行升序排序
select * from tb_emp order by entrydate ASC; #升序为默认排序方式 asc可以省略
#2.根据入职日期,对员工进行降序排序
select * from tb_emp order by entrydate DESC;
#3.根据入职时间对公司的员工进行升序,入职时间相同,再按照更新时间进行降序
select * from tb_emp order by entrydate ASC,update_time DESC;
语法
select 字段列表 from 表名 limit 起始索引,查询记录数;
练习
#1.从其实索引0开始查询员工数据,每页展示5条
select * from tb_emp limit 0,5;
#2.查询第一页员工数据,每页展示五条
select * from tb_emp limit 0,5;
#3.查询第二页员工数据,每页展示5条
select * from tb_emp limit 5,5;
#4.查询第三页员工数据,每页展示5条
select * from tb_emp limit 10,5;
select * from tb_emp limit 5*(页数——1),5
【注意事项】
函数
if (表达式,true,false)
case 字段名 when 值1 then result [when 值2 then result2...] [else result] end;
案例
-- 案例:按需求完成员工管理的条件分页查询, 根据输入条件,查询第一页数据,每页展示10条记录
-- 输入条件:姓名 张,性别 男,入职日期 2000-01-01 - 2015-12-31
select * from tb_emp where name like '%张%' and gender = 1 and entrydate between '2000-01-01' and '2015-12-31'
order by update_time DESC limit 0,10;
案例
-- 统计出男性和女性 分别有多少人,并且添加别名
-- 数据库的性别为1 和 2 来标识性别,使用if 函数
select if(gender=1,'男','女') 性别,count(*) from tb_emp group by gender;
案例
-- 统计出员工职位,并统计在该职位有多少人
-- 数据库的职位为1 班主任,2 讲师,3 学工主管,4 教研主管 使用case when 函数
select
case job when 1 then '班主任' when 2 then '讲师' when 3 then '学工主管' when 4 then '教研主管' else '没有分配职位' end
as 职位,
count(*) from tb_emp group by job;
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 '修改时间'
) comment '部门表';
一对多关系实现:在数据库中多的以防,添加字段,来关联一的一方主键
现象
分析
外键约束
创建表时指定
create table 表名(
字段名 数据类型,
...,
[constraint] [外键名称] foreign key(外键字段名) reference 主表(字段名)
)
建表后,添加外键
alert table 表名 add contraint 外键名称 foreign key(外键字段名) reference 主表(字段名);
物理外键
逻辑外键【推荐方式】
-- 瑞吉点餐页面原型 , 设计表结构
-- 分类表
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 not null default 0 comment '状态 0 禁用,1 启用',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '更新时间'
) comment '菜品及套餐分类' ;
-- 菜品表
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 '菜品分类ID',
price decimal(8, 2) not null comment '菜品价格',
image varchar(300) not null comment '菜品图片',
description varchar(200) comment '描述信息',
status tinyint unsigned not null default 0 comment '状态, 0 停售 1 起售',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '更新时间'
) comment '菜品';
-- 套餐表
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 '分类id',
price decimal(8, 2) not null comment '套餐价格',
image varchar(300) not null comment '图片',
description varchar(200) comment '描述信息',
status tinyint unsigned not null default 0 comment '状态 0:停用 1:启用',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '更新时间'
)comment '套餐' ;
-- 套餐菜品关联表
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 tinyint unsigned not null comment '份数'
)comment '套餐菜品关系';