一、查询语法介绍
# 1、查询多个字段
语法 SELECT 字段1,字段2,字段3…… FROM 表名;
语法 SELECT * 表名;
# 2、设置别名 ,AS可省略
语法 SELECT 字段1[AS别名1],字段2[AS别名2],字段3[AS别名3] …… FROM 表名;
# 3、删除重复数据
语法 SELECT DISTINCT 字段列表 FROM 表名;
二、简单查询
-- 数据准备
# 切换到itcast数据库
use itcast
# 创建表
CREATE TABLE emp (
`id` int NULL COMMENT '编号',
`workno` varchar(10) NULL COMMENT '工号',
`name` varchar(10) NULL COMMENT '姓名',
`gender` char(1) NULL COMMENT '姓别',
`age` tinyint unsigned NULL COMMENT '年龄',
`idcard` char(18) NULL COMMENT '身份证号',
`workaddress` varchar(255) NULL COMMENT '工作地址',
`entydate` date COMMENT '入职时间'
) COMMENT = '员工表';
# 插入数据
insert into epm(id,workno,name,gender,age,idcard,workaddress,entydate)
values(………………)
+------+--------+--------+--------+------+--------------------+-------------+------------+
| id | workno | name | gender | age | idcard | workaddress | entydate |
+------+--------+--------+--------+------+--------------------+-------------+------------+
| 1 | 10001 | 罗时 | 男 | 32 | 110115199010012899 | 北京 | 2007-00-20 |
| 2 | 10002 | 余尧 | 男 | 23 | 110115199910013971 | 上海 | 2006-09-20 |
| 3 | 10003 | 熊彪 | 男 | 30 | 110115199210013881 | 武汉 | 2010-02-20 |
| 4 | 10004 | 秦良 | 男 | 35 | 110115198710013764 | 北京 | 2003-07-20 |
| 5 | 10005 | 范炎 | 男 | 13 | 110115200910012611 | 福州 | 2009-09-20 |
| 6 | 10006 | 钱家 | 男 | 32 | 110115199010013379 | 武汉 | 2010-00-20 |
| 7 | 10007 | 崔安 | 男 | 12 | 110115201010012183 | 北京 | 2010-00-20 |
| 8 | 10008 | 彭之饭 | 男 | 28 | 110115199410012222 | 上海 | 2009-04-20 |
| 9 | 10009 | 朱昱 | 男 | 27 | 11011519951001336X | 江苏 | 2002-05-20 |
| 10 | 10010 | 叶谊 | 男 | 21 | 110115200110013837 | 厦门 | 2009-01-20 |
| 11 | 10011 | 韩艳 | 女 | 12 | 110115201010013728 | 北京 | 2004-00-20 |
| 12 | 10012 | 夏咛 | 女 | 28 | 110115199410013621 | 上海 | 2004-04-20 |
| 13 | 10013 | 顾英 | 女 | 14 | 110115200810011376 | 上海 | 2002-08-20 |
| 14 | 10014 | 朱丽云 | 女 | 20 | 110115200210013016 | 福州 | 2008-02-20 |
| 15 | 10015 | 姜盈 | 女 | 25 | 11011519971001145X | 武汉 | 2009-07-20 |
| 16 | 10016 | 顾心林 | 女 | 28 | 110115199410011314 | 北京 | 2012-04-20 |
| 17 | 10017 | 薛烟 | 女 | 25 | 110115199710011583 | 上海 | 2008-07-20 |
| 18 | 10018 | 吴媱 | 女 | 36 | 110115198610012222 | 北京 | 2001-06-20 |
| 19 | 10019 | 萧俪 | 女 | 12 | 110115201010012009 | 武汉 | 2003-00-20 |
| 20 | 10020 | 邓菊 | 女 | 31 | NULL | 福州 | 2006-01-20 |
+------+--------+--------+--------+------+--------------------+-------------+------------+
# 1、查询指定字段name,workno,age返回
select name,workno,age from emp;
# 2、查询所有字段并返回,项目中尽量写出查询字段,方便后期维护
select id, workno, name, gender,age,idcard,workaddress,entydate from emp;
select * from emp;
# 3、查询所有员工工作地址,取别名。as可以省略
select workaddress as '工作地址'from emp;
select workaddress '工作地址'from emp;
# 4、查询员工的上班地址(不要重复)
select distinct workaddress from emp;
三、条件查询WHERE
语法 select 字段列表 from 表名 where 条件列表;
-- 1、查询年龄=20的员工
select * from emp where age=20;
-- 2、查询年龄>20岁的员工
select * from emp where age>20;
-- 3、查询年龄<=20岁的员工
select * from emp where age<=20;
-- 4、查询没有身份证号的员工
select * from emp where idcard is null;
-- 5、查询有身份证号的员工
select * from emp where idcard is not null;
-- 6、查询年龄不等于20岁的员工
select * from emp where age<>20;
select * from emp where age!=20;
-- 7、查询年龄15-20岁的员工,包含15,20
select * from emp where age>=15 && age<=20;
select * from emp where age>=15 and age<=20;
# between 后最小值,and后跟的是最大值
select * from emp where age between 15 and 20;
-- 8、性别为女,且年龄小于20岁
select * from emp where gender='女' and age<20;
-- 9、查询年龄等于18 or 28 or 30
select * from emp where age=18 or age=28 or age=30;
select * from emp where age in (18,28,30);
-- 10、查询名字为三个字的员工
select * from emp where name like '朱_';
select * from emp where name like '朱%';
-- 11、身份证号最后一位为X的员工
select * from emp WHERE IDCARD LIKE '%X';
select * from emp WHERE IDCARD LIKE '_________________X';
四、聚合函数
-- 1、聚合函数 作用于某一列 null不参与运算
-- count,max,min,avg,sum
语法 select 聚合函数(字段列表)from 表名;
-- 1、统计该企业员工数量
select count(*) from emp;
select count(id) from emp;
select count(idcard) from emp;
-- 2、统计员工平均年龄
select avg(age) from emp;
-- 3、统计员工最大年龄
select max(age) from emp;
-- 4、统计员工最小年龄
select min(age) from emp;
-- 5、统计武汉区域所有员工年龄之和
select * from emp where workaddress = '武汉';
select sum(age) from emp where workaddress = '武汉';
五、分组查询(group by)
语法 select 字段列表 from 表名 [where 分组前过滤条件] group by 分组字段名 [having 分组后过滤条件]
-- where和having区别
-- 1、执行时机不同,where是分组之前过滤,不满足where的条件,不参与分组;而having是分组之后对结果进行过滤。
-- 2、判断条件不同,where不能对聚合函数进行判断,而having可以
-- 1、根据性别分组,统计为男性员工和女性员工的数量。
select count(*) from emp group by gender;
select gender, count(*) from emp group by gender;
-- select gender,name, count(*) from emp group by gender,name;
-- 2、根据性别分组,统计为男性运功和女性员工的平均年龄。
select gender, avg(age) from emp group by gender;
-- 3、查询年龄小于25岁的员工,并根据工作地址分组,获取员工大于等于3的工作地址。
select * from emp where age<45;
select workaddress, count(*) from emp where age<45 group by workaddress having count(*)>=3;
# 别名用法
select workaddress, count(*)as address_count from emp where age<45 group by workaddress having address_count>=3;
六、排序查询order by
语法 select 字段列表 form 表名 order by 字段1 排序方式1,字段2 排序方式2……;
-- 排序方式
-- asc:升序(默认值)
-- desc:降序
-- 多字段排序,当第一个字段值相同,才会根据第二个字段排序。
-- 1、根据年龄对公司的员工进行升序排序
select * from emp order by age;
select * from emp order by age asc;
select * from emp order by age desc;
-- 2、根据入职时间对员工进行降序排序
select * from emp order by entydate desc;
-- 3、根据年龄对公司员工进行升序排序,年龄相同,再根据入职时间进行降序排序。
select * from emp order by age asc , entydate desc;
-- 先年龄升序,年龄相同,再入职升序
select * from emp order by age asc , entydate asc;
select * from emp order by age , entydate;
七、分页查询 LIMIT
语法 select 字段列表 from 表名 limit 起始索引,查询记录数;
语法 select 字段列表 from 表名 [where 条件] [order by 字段1 排序方式1,字段2 排序方式2]limit 起始索引,查询记录数;
注意:
起始索引从0开始,起始索引=(查询页码-1)*每页显示记录数。
分页查询是数据的方言,不同的数据库有不同的方法,mysql使用的是limit。
如果查询的是第一页数据,起始索引是可以省略的,直接简写limit 10。
-- 1、查询第一页的员工数据,每页展示10条数据
select * from emp limit 0, 10;
select * from emp limit 10;
-- 2、查询第二页员工数据,每页展示10条数据
select * from emp limit 3, 3;
select * from emp limit 10, 10;
八、DQL案例
-- 1、查询年龄为10,21,22,23岁的员工信息
select * from emp where age in (10,21,22,23);
-- 2、查询性别为男,并且年龄在20-40岁(含)以内的姓名为三个字的员工
select * from emp where gender = '男' and (age between 20 and 40) and name like '___';
-- 3、统计员工表中,年龄小于30岁的,男性员工和女性员工的人数。
select* from emp where age<30;
select gender, count(gender) from emp where age<30 group by gender;
-- 4、查询所有年龄小于等于25岁员工姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按入职时间降序排序
select name,age from emp where age<=25 order by age asc,entydate desc;
-- 5、查询性别为女,且年龄在10-30岁(含)以内的前5个员工信息,对查询的结果按年龄升序排序,年龄相同的按入职时间升序排序。
select * from emp where gender = '女' and (age between 10 and 30) order by age asc, entydate asc limit 0,5;
九、DQL语句执行顺序
执行顺序:
4 select 字段列表
1 from 表名列表
2 where 条件列表
3 group by 分组字段列表
having 分组后条件列表
5 order by 排序字段列表
6 limit 分页参数
-- 查询年龄大于15的员工姓名,年龄,根据年龄进行升序排序。
select e.name,e.age from emp as e where e.age>15 order by age;