单表查询
单表查询的语法和优先级
单表查询的顺序不能改变(有没有无所谓)
SELECT distinct(去重) 字段1,字段2... FROM 表名
where 约束条件
group by 分组的字段
having 过滤条件 (是在分组之后进行过滤的)
select 选择 (这个就是放在开头)
distinct 去重
order by 排序字段
limit 限制条件;
select 选择字段
distinct 是否去重
from 选择是哪张表 where 指定约束条件 group by 按照字段分组 不分组默认整体是一组 having 分组的结果可以进行 过滤 order by 进行条件排序 limit 限制显示条数
concat 聚合函数
company.employee 员工id id int 姓名 emp_name varchar 性别 sex enum 年龄 age int 入职日期 hire_date date 岗位 post varchar 职位描述 post_comment varchar 薪水 salary double 办公室 office int 部门编号 depart_id int #创建表 create table employee( id int not null unique auto_increment, name varchar(20) not null, sex enum('male','female') not null default 'male', #大部分是男的 age int(3) unsigned not null default 28, hire_date date not null, post varchar(50), post_comment varchar(100), salary double(15,2), office int, #一个部门一个屋子 depart_id int ); #查看表结构 mysql> desc employee; +--------------+-----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+-----------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | sex | enum('male','female') | NO | | male | | | age | int(3) unsigned | NO | | 28 | | | hire_date | date | NO | | NULL | | | post | varchar(50) | YES | | NULL | | | post_comment | varchar(100) | YES | | NULL | | | salary | double(15,2) | YES | | NULL | | | office | int(11) | YES | | NULL | | | depart_id | int(11) | YES | | NULL | | +--------------+-----------------------+------+-----+---------+----------------+ #插入记录 #三个部门:教学,销售,运营 insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values ('egon','male',18,'20170301','驻沙河办事处外交大使',7300.33,401,1), #以下是教学部 ('alex','male',78,'20150302','teacher',1000000.31,401,1), ('wupeiqi','male',81,'20130305','teacher',8300,401,1), ('yuanhao','male',73,'20140701','teacher',3500,401,1), ('liwenzhou','male',28,'20121101','teacher',2100,401,1), ('jingliyang','female',18,'20110211','teacher',9000,401,1), ('jinxin','male',18,'19000301','teacher',30000,401,1), ('成龙','male',48,'20101111','teacher',10000,401,1), ('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门 ('丫丫','female',38,'20101101','sale',2000.35,402,2), ('丁丁','female',18,'20110312','sale',1000.37,402,2), ('星星','female',18,'20160513','sale',3000.29,402,2), ('格格','female',28,'20170127','sale',4000.33,402,2), ('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门 ('程咬金','male',18,'19970312','operation',20000,403,3), ('程咬银','female',18,'20130311','operation',19000,403,3), ('程咬铜','male',18,'20150411','operation',18000,403,3), ('程咬铁','female',18,'20140512','operation',17000,403,3) ;
单表查询简单用法
select post from employee;
去重:distinct
select distinct post from employee;
mysql> select distinct post from employee;
+----------------------------+
| post |
+----------------------------+
| 老男孩驻沙河办事处外交大使 |
| teacher |
| sale |
| operation |
+----------------------------+
4 rows in set (0.01 sec)
可以选择字段直接进行四则运算
select name,salary*12 from employee;
mysql> select name,salary*12 from employee;
+------------+-------------+
| name | salary*12 |
+------------+-------------+
| egon | 87603.96 |
| alex | 12000003.72 |
| 程咬银 | 228000.00 |
| 程咬铜 | 216000.00 |
| 程咬铁 | 204000.00 |
+------------+-------------+
18 rows in set (0.01 sec
select name,salary*12 as 年薪 from employee;
查询并修改显示名称
+------------+-------------+
| name | 年薪 |
+------------+-------------+
| egon | 87603.96 |
| alex | 12000003.72 |
| wupeiqi | 99600.00 |
+------------+-------------+
定义显示格式
concat()函数用于连接字符串
select concat('名字: ',name,' ','年薪: ',salary*12 )from employee;
直接小括号内进行字符串的查询和拼接
+-----------------------------------------------+
| concat('名字: ',name,' ','年薪: ',salary*12 ) |
+-----------------------------------------------+
| 名字: egon 年薪: 87603.96 |
| 名字: alex 年薪: 12000003.72 |
| 名字: wupeiqi 年薪: 99600.00 |
select concat(name,':',salary*12 )from employee;
直接名字加年薪
| egon:87603.96 |
| alex:12000003.72 |
| wupeiqi:99600.00 |
select concat(name,':',salary*12,':',age,':',sex )from employee;
添加直接往后加就可以了
concat_ws 第一个参数为分隔符
select concat_ws(':',name,salary*12)from employee;
直接定义分割符
练习
1 查出所有员工的名字,薪资,格式为 <名字:egon> <薪资:3000> 2 查出所有的岗位(去掉重复) 3 查出所有员工名字,以及他们的年薪,年薪的字段名为annual_year select concat('<名字:',name,'> ','<薪资:',salary,'>') from employee; select distinct depart_id from employee; select name,salary*12 annual_salary from employee;
单表查询 where约束
- where + 字段名 +比较运算符:> < >= <= <> != 和 逻辑符 and or not
- between 数值 and 数值 范围查询多少到多少
- in(10,20,30)值 查找符合条件的显示
- like 模糊查询 以什么开头 + %代表任意字符 或者 _ 表示一个字符
where 约束条件
select* from employee;
查出来老师都有哪些?
where 指定字段名进行查询
select* from employee where post='teacher';
只查老师的部分信息
select id,name from employee where post='teacher';
查出来id 3到5 之间的信息,指定约束条件即可
<比较运算符 <>= != 和 and or>
select id,name from employee where id>=3 and id<5;
between 数值 and 数值 选择范围进行查询
select id,name from employee where between 3 and 5;
选择一个字段的多条结果进行查询
select id,name,age from employee
where age=78 or age=18 or age=60;
查找字段多条结果的简便写法,只有符合条件的会显示出来
select id,name,age from employee
where age in (60,70,80,18,'aaa');
like 模糊查询匹配 % 或者_ (表示一个字符)
查询名字以e开头
select id,name,age from employee
where name like 'e%';
查询名字是三个字符的
select id,name,age from employee
where name like '___';
not 加在关键字前 即可
is 查询指定字段是否是空
select * from employee where post_comment is null ;
#1:单条件查询 SELECT name FROM employee WHERE post='sale'; #2:多条件查询 SELECT name,salary FROM employee WHERE post='teacher' AND salary>10000; #3:关键字BETWEEN AND SELECT name,salary FROM employee WHERE salary BETWEEN 10000 AND 20000; SELECT name,salary FROM employee WHERE salary NOT BETWEEN 10000 AND 20000; #4:关键字IS NULL(判断某个字段是否为NULL不能用等号,需要用IS) SELECT name,post_comment FROM employee WHERE post_comment IS NULL; SELECT name,post_comment FROM employee WHERE post_comment IS NOT NULL; SELECT name,post_comment FROM employee WHERE post_comment=''; 注意''是空字符串,不是null ps: 执行 update employee set post_comment='' where id=2; 再用上条查看,就会有结果了 #5:关键字IN集合查询 SELECT name,salary FROM employee WHERE salary=3000 OR salary=3500 OR salary=4000 OR salary=9000 ; SELECT name,salary FROM employee WHERE salary IN (3000,3500,4000,9000) ; SELECT name,salary FROM employee WHERE salary NOT IN (3000,3500,4000,9000) ; #6:关键字LIKE模糊查询 通配符’%’ SELECT * FROM employee WHERE name LIKE 'eg%'; 通配符’_’ SELECT * FROM employee WHERE name LIKE 'al__'; 小练习: 1. 查看岗位是teacher的员工姓名、年龄 2. 查看岗位是teacher且年龄大于30岁的员工姓名、年龄 3. 查看岗位是teacher且薪资在9000-1000范围内的员工姓名、年龄、薪资 4. 查看岗位描述不为NULL的员工信息 5. 查看岗位是teacher且薪资是10000或9000或30000的员工姓名、年龄、薪资 6. 查看岗位是teacher且薪资不是10000或9000或30000的员工姓名、年龄、薪资 7. 查看岗位是teacher且名字是jin开头的员工姓名、年薪 select name,age from employee where post = 'teacher'; select name,age from employee where post='teacher' and age > 30; select name,age,salary from employee where post='teacher' and salary between 9000 and 10000; select * from employee where post_comment is not null; select name,age,salary from employee where post='teacher' and salary in (10000,9000,30000); select name,age,salary from employee where post='teacher' and salary not in (10000,9000,30000); select name,salary*12 from employee where post='teacher' and name like 'jin%';
分组查询:GROUP BY 与 聚合函数
没有分组默认整体一组
分组查询group by 分组查询 (先分完组才好统计数量)
group_concat()一起使用的函数
强调 分组之后select只能查看分组的字段 ,要想查看组内内容不能直接查看
需要借助聚合函数 max ,min,avg,sum,count
分组的目的是为了以组为单位来处理记录,而不是处理单独的记录
分组依据 多条记录有相同字段 如部门 ,年龄, 性别
select count(id) from employee;
先根据条件进行分组 然后根据要查询的数量进行查询 查看各部门员工数
select post,count(id) from employee group by post;
这么查没有意义只查这个组的第一条记录
select post,name from employee group by post;
查看teacher部门的员工数
select post,count(id) from employee where post='teacher';
聚合函数 #强调:聚合函数聚合的是组的内容,若是没有分组,则默认一组
select post,max(salary) from employee group by post;
select post,min(salary) from employee group by post;
select post,avg(salary) from employee group by post;
select post,sum(salary) from employee group by post;
查看各部门都有哪些员工
select post,group_concat(name) from employee group by post;
可以加字符串拼接
select post,group_concat('薪资,salary') from employee group by post;
二 ONLY_FULL_GROUP_BY 完整的分组限制
#查看MySQL 5.7默认的sql_mode如下: mysql> select @@global.sql_mode; ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION #!!!注意 ONLY_FULL_GROUP_BY的语义就是确定select target list中的所有列的值都是明确语义,简单的说来,在ONLY_FULL_GROUP_BY模式下,target list中的值要么是来自于聚集函数的结果,要么是来自于group by list中的表达式的值。 #设置sql_mole如下操作(我们可以去掉ONLY_FULL_GROUP_BY模式): mysql> set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
mysql> select @@global.sql_mode; +-------------------+ | @@global.sql_mode | +-------------------+ | | +-------------------+ 1 row in set (0.00 sec) mysql> select * from emp group by post; +----+------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+ | id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id | +----+------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+ | 14 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 | | 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 | | 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 | | 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 | +----+------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+ 4 rows in set (0.00 sec) #由于没有设置ONLY_FULL_GROUP_BY,于是也可以有结果,默认都是组内的第一条记录,但其实这是没有意义的 mysql> set global sql_mode='ONLY_FULL_GROUP_BY'; Query OK, 0 rows affected (0.00 sec) mysql> quit #设置成功后,一定要退出,然后重新登录方可生效 Bye mysql> use db1; Database changed mysql> select * from emp group by post; #报错 ERROR 1055 (42000): 'db1.emp.id' isn't in GROUP BY mysql> select post,count(id) from emp group by post; #只能查看分组依据和使用聚合函数 +----------------------------+-----------+ | post | count(id) | +----------------------------+-----------+ | operation | 5 | | sale | 5 | | teacher | 7 | | 老男孩驻沙河办事处外交大使 | 1 | +----------------------------+-----------+ 4 rows in set (0.00 sec)这
详解 GROUP BY #1、首先明确一点:分组发生在where之后,即分组是基于where之后得到的记录而进行的 #2、分组指的是:将所有记录按照某个相同字段进行归类,比如针对员工信息表的职位分组,或者按照性别进行分组等 #3、为何要分组呢? 取每个部门的最高工资 取每个部门的员工数 取男人数和女人数 小窍门:‘每’这个字后面的字段,就是我们分组的依据 #4、大前提: 可以按照任意字段分组,但是分组完毕后,比如group by post,只能查看post字段,如果想查看组内信息,需要借助于聚合函数 使用方法: 单独使用GROUP BY关键字分组 SELECT post FROM employee GROUP BY post; 注意:我们按照post字段分组,那么select查询的字段只能是post,想要获取组内的其他相关信息,需要借助函数 GROUP BY关键字和GROUP_CONCAT()函数一起使用 SELECT post,GROUP_CONCAT(name) FROM employee GROUP BY post;#按照岗位分组,并查看组内成员名 SELECT post,GROUP_CONCAT(name) as emp_members FROM employee GROUP BY post; GROUP BY与聚合函数一起使用 select post,count(id) as count from employee group by post;#按照岗位分组,并查看每个组有多少人 练习: 1. 查询岗位名以及岗位包含的所有员工名字 2. 查询岗位名以及各岗位内包含的员工个数 3. 查询公司内男员工和女员工的个数 4. 查询岗位名以及各岗位的平均薪资 5. 查询岗位名以及各岗位的最高薪资 6. 查询岗位名以及各岗位的最低薪资 7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资 select post,group_concat(name) from employee group by post; select post,count(id) from employee group by post; select sex,count(id) from employee group by sex; select sex,avg(salary)from employee group by sex;
Having 过滤
having 过滤条件 只能放在分组的后面,一般过滤用where足可
和where区别是优先级 聚合函数
select * from employee where salary > 10000;
#错误:having是在分组之后的,意味着,只能用分组的字段或者聚合函数作为过滤条件
select post from employee
group by post
having salary > 10000
;
select * from employee
having salary > 10000;
select * from employee
having count(id) > 10000;
#取出员工数大于3的部门
select post from employee group by post having count(id) > 3;
#取出id是大于10的,员工数大于3的部门
select post from employee where id > 10 group by post having count(id) > 3;
#练习
1. 查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数
select post,group_concat(name),count(id) from employee group by post having count(id) < 2;
3. 查询各岗位平均薪资大于10000的岗位名、平均工资
select post,avg(salary) from employee group by post having avg(salary) > 10000;
4. 查询各岗位平均薪资大于10000且小于20000的岗位名、平均工资
select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary) < 20000;
查询排序:ORDER BY
排序只有升序和降序 两种排列方式 可以指定字段进行升降排序
#默认升序 select * from employee order by age asc; select * from employee order by age; #降序 select * from employee order by age desc; #先按照age升序排,如果age相同,则id降序排序 select * from employee order by age asc,id desc; #查看每个部门的最高工资 select post,max(salary) from employee group by post order by max(salary); select post,max(salary) as m from employee group by post order by m; #取出平均工资>10000的部门以及它的平均工资 select post,avg(salary) from employee group by post having avg(salary) > 10000; #错误,执行having时,select还没有执行呢 select post,avg(salary) as x from employee group by post having x > 10000; #取出平均工资>10000的部门以及它的平均工资 select post,avg(salary) as x from employee group by post having avg(salary) > 10000 order by x desc ; select post,avg(salary) as x from employee group by post having avg(salary) > 10000 order by avg(salary) desc ; #错误 select post,avg(salary) as x from employee group by post having avg(salary) > 10000 order by post ; select post,avg(salary) as x from employee group by post having avg(salary) > 10000 order by x ;
限制查询的记录数:LIMIT
主要是用于翻页 限制显示
select * from employee limit 3;
#取出工资最高的那个员工信息
select * from employee order by salary desc limit 1;
显示一条记录 最高的两人就显示两条记录
#总共的计数数:30123
#一页只能显示:30
select * from employee limit 0,3; 从0开始 向后查3条
select * from employee limit 3,3;从3开始 向后查3条
select * from employee limit 6,3;从6开始 向后查3条
正则表达式查询
SELECT * FROM employee WHERE name REGEXP '^ale'; SELECT * FROM employee WHERE name REGEXP 'on$'; SELECT * FROM employee WHERE name REGEXP 'm{2}'; 小结:对字符串匹配的方式 WHERE name = 'egon'; WHERE name LIKE 'yua%'; WHERE name REGEXP 'on$'; 查看所有员工中名字是jin开头,n或者g结果的员工信息 select * from employee where name regexp '^jin.*[gn]$';