目录
1、基础查询
1. 查询多个字段
1. 举例
2. 举例
2. 字段设置别名
1. 举例
2. 举例
3. 去除重复记录
1. 举例
2、条件查询
1. 等于(=)
2. 小于(<)
3. 小于等于(<=)
4. 没有(is null)
5. 有(is not null)
6. 不等于(!=或者<>)
7. 并且/在某个范围内(&&或者and或者between...and..)
8. IN(...)在in之后的列表中的值,多选一
9. like 占位符
3、聚合函数
1. 介绍
2. 常见的聚合函数
3. 语法
4. 举例
1. 统计员工数量
2. 平均、最大、最小以及和
4、分组查询
1. 语法
2. where和having的区别
3. 注意
4. 举例
1. 根据性别分组 , 统计男性员工和女性员工的数量
2. 根据性别分组 , 统计男性员工和女性员工的平均年龄
3. 查询年龄小于45的员工 , 并根据工作地址分组 , 获取员工数量大于等于3的工作地址
4. 统计各个工作地址上班的男性及女性员工的数量
5、排序查询
1. 语法
2. 排序方式
3. 注意
4. 举例
1. 根据年龄对公司的员工进行升序排序
2. 根据入职时间, 对员工进行降序排序
3. 根据年龄对公司的员工进行升序排序 , 再按照入职时间进行降序排序
6、分页查询
1. 语法
2. 举例
1. 查询第1页员工数据, 每页展示10条记录
create table emp(
id int comment '编号',
workno varchar(10) comment '工号',
name varchar(10) comment '姓名',
gender char(1) comment '性别',
age tinyint unsigned comment '年龄',
idcard char(18) comment '身份证号',
workaddress varchar(50) comment '工作地址',
entrydate date comment '入职时间'
)comment '员工表';
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (1, '00001', '柳岩666', '女', 20, '123456789012345678', '北京', '2000-01-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (2, '00002', '张无忌', '男', 18, '123456789012345670', '北京', '2005-09-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (3, '00003', '韦一笑', '男', 38, '123456789712345670', '上海', '2005-08-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (4, '00004', '赵敏', '女', 18, '123456757123845670', '北京', '2009-12-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (5, '00005', '小昭', '女', 16, '123456769012345678', '上海', '2007-07-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (6, '00006', '杨逍', '男', 28, '12345678931234567X', '北京', '2006-01-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (7, '00007', '范瑶', '男', 40, '123456789212345670', '北京', '2005-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (8, '00008', '黛绮丝', '女', 38, '123456157123645670', '天津', '2015-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (9, '00009', '范凉凉', '女', 45, '123156789012345678', '北京', '2010-04-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (10, '00010', '陈友谅', '男', 53, '123456789012345670', '上海', '2011-01-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (11, '00011', '张士诚', '男', 55, '123567897123465670', '江苏', '2015-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (12, '00012', '常遇春', '男', 32, '123446757152345670', '北京', '2004-02-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (13, '00013', '张三丰', '男', 88, '123656789012345678', '江苏', '2020-11-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (14, '00014', '灭绝', '女', 65, '123456719012345670', '西安', '2019-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (15, '00015', '胡青牛', '男', 70, '12345674971234567X', '西安', '2018-04-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (16, '00016', '周芷若', '女', 18, null, '北京', '2012-06-01');
select 字段1, 字段2, 字段3 ... from 表名 ;
#查询全部字段信息
select *from 表名;
#查询编号(id),工号(workno),年龄(age)
select id,workno,age from emp;
select *from emp;
select 字段1 [ as 别名1 ] , 字段2 [ as 别名2 ] ... from 表名;
select 字段1 [ 别名1 ] , 字段2 [ 别名2 ] ... from 表名;
select workno as '工作编号' from emp;
select name as '姓名' ,workaddress as '工作地点'from emp;
select distinct 字段列表 from 表名;
select distinct workaddress from emp;
select 字段列表 from 表名 where 条件列表 ;
select * from emp where age = 88;
select * from emp where age < 20;
查询年龄小于等于 20 的员工信息
select * from emp where age <= 20;
select * from emp where idcard is null;
select * from emp where idcard is not null;
查询年龄不等于 88 的员工信息
select * from emp where age !=88;
select * from emp where age<>88;
select * from emp where age >= 15 && age <= 20;
select * from emp where age >= 15 and age <= 20;
select * from emp where age between 15 and 20;
select * from emp where gender='女' and age<25;
select * from emp where age=18 or age=20 or age=40;
select * from emp where age in(18,20,40);
select * from emp where idcard like '_________________x';
将一列数据作为一个整体,进行纵向计算
函数 | 功能 |
count | 统计数量 |
max | 最大值 |
min | 最小值 |
avg | 平均值 |
sum | 求和 |
SELECT 聚合函数(字段列表) FROM 表名 ;
select count(*) from emp; -- 统计的是总记录数
select count(idcard) from emp; -- 统计的是idcard字段不为null的记录数
-- 统计员工的平均年龄
select avg(age) from emp;
-- 统计最大年龄
select max(age) from emp;
-- 统计最小年龄
select min(age) from emp;
-- 统计西安地区员工的年龄之和
select sum(age) from emp where workaddress ='西安';
SELECT 字段列表 FROM 表名 [ WHERE 条件 ] GROUP BY 分组字段名 [ HAVING 分组
后过滤条件 ];
select gender,count(*) from emp group by gender;
select gender,avg(age) from emp group by gender;
select workaddress,count(*) adress_c from emp where age<45 group by workaddress having adress_c>=3;
select workaddress, gender, count(*) '数量' from emp group by workaddress , gender;
select 字段列表 from表名 order by 字段1 排序方式1 , 字段2 排序方式2 ;
select * from emp order by age asc;
select * from emp order by age;
select * from emp order by entrydate desc;
select * from emp order by age asc , entrydate desc;
SELECT 字段列表 FROM 表名 LIMIT 起始索引, 查询记录数 ;
select * from emp limit 0,10;
select * from emp limit 10;