MySQL单表查询

前期表准备,创建一张员工表,往里面插入数据

create table emp(
  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
);

#插入记录
#三个部门:教学,销售,运营
insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values
('jason','male',18,'20170301','张江第一帅形象代言',7300.33,401,1), #以下是教学部
('egon','male',78,'20150302','teacher',1000000.31,401,1),
('kevin','male',81,'20130305','teacher',8300,401,1),
('tank','male',73,'20140701','teacher',3500,401,1),
('owen','male',28,'20121101','teacher',2100,401,1),
('jerry','female',18,'20110211','teacher',9000,401,1),
('nick','male',18,'19000301','teacher',30000,401,1),
('sean','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 id,name from emp where id > 3 and id <6;     查询emp表id大于3且小于6的id,name

执行顺序:

from   #先确定是哪张表
where    #根据过来的条件  筛选数据
select    #拿筛选出来的数据中的某些字段显示出来,select是查询后要显示的字段

一、where约束条件

1.查询id小于等于3大于等于6的数据   (根据执行顺序可以先写from,然后写where,最后写select)

select * from emp where id >=3  and id <=6;
sleect * from emp where id between 3 and 6;    #这两句完全等价

2.查询薪资是20000或者18000或者17000的数据

select * from emp where salary=20000 or salary=18000 or salary=17000;
select * from emp where salary in (20000,18000,17000);

3.查询员工姓名中包含o字母的员工姓名和薪资

模糊匹配:  like

%:匹配多个任意字符

_:匹配一个任意字符

select name,salary from emp where name like '%o%';

4.查询员工姓名是由四个字符组成的员工姓名与薪资

select name,salary from emp where name like '____';

5.查询id小于3或者大于6的数据

select * from emp where id <3 or id >6;
select * from emp where id not between 3 and 6;

6.查询薪资不在20000,18000,17000范围的数据

select *  from emp where salary not in (20000,18000,17000);

7.查询岗位描述为空的员工与岗位名            针对null判断的时候只能用is,不能用 =

select name,post_comment from emp where post_comment  is null;
select name,post_comment from emp where post_comment  = null;   #报错

执行顺序:  from>>where>>select

二、group by   分组

1.按部门分组    group by后面写的是分组的依据(某个字段)

select * from emp group by post;     分组之后应该做到最小单位是组,而不应该是展示组内的单个数据信息。

Mysql分组之后,只能拿到组的字段信息,无法直接获取组内其他字段信息,但是可以通过其他方法(聚合函数)间接的获取。聚合函数只能在“分组”之后使用,如果整个sql语句没有group by默认整体就是一组。

上面的语句如果你的mysql没有报错,说明严格模式没有设置,在严格模式中加入  only_full_group_by,然后重新开一个cmd

#操作
show variables like '%mode%'; set global sql_mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,only_full_group_by";

正确部门分组语句

select post from emp group by post;  #获取部门信息

强调:只要分组了,就不能够再‘直接’查找到单个数据信息了,只能获取到组名。

2.获取每个部门的最高工资

以组为单位统计组内数据>>>聚合查询(聚集到一起合成为一个结果)

select post,max(salary) from emp group by post;

3.获取每个部门最低工资

select post,min(salary) from emp group by post;

4.获取每个部门的平均工资

select post,avg(salary) from emp group by post;

5.获取每个部门的工资总和

select post,sum(salary) from emp group by post;

6.每个部门的人数

select post,count(id) from emp group by post;   #根据id来算人数
select post,count(age) from emp group by post;
select post,count(salary) from emp group by post;
select post,count(id) from emp group by post;
select post,count(post_comment) from emp group by post;

在统计分组内个数的时候 填写任意非空字段都可以完成计数,推荐使用能够唯一标识数据的字段比如id字段

聚合函数:max,min,avg,sum,count    聚合函数会自动将每一个分组内的单个数据做想要的计算,无需你考虑。

前面说到分组之后不能直接获取到组内的详细信息,可以用group_concat(字段名)用来显示组内的详细字段值(字段也可以是多个),还可以用来拼接字符串。 

7.查询分组之后的部门名称和每个部门下所有的员工姓名         

select post,group_concat(name) from emp group by post;

select post,group_concat(name,age) from emp group by post;    #查询多个字段

字符串拼接:

select post,group_concat(name,':',salary) from emp group by post;

8.补充concat()  (和group_concat()的区别在于,concat()用在不分组的情况下,也可以查询字段和拼接字符串)       as语法使用(as不仅可以给字段起别名还可以给表起别名)

select concat('NAME:',name) as 姓名,concat('SAL:',salary) as 薪资  from emp;

9.查询四则运算,查询每个人的年薪

select name,salary*12 from emp;

 总结:

 刚开始查询表时,一定要按照最基本的步骤,先确定是哪张表(from),再确定查这张表有没有限制条件(where),再确定是否需要分类(group by),最后再确定需要什么字段对应的信息(select)

执行顺序:from >> where >> group by >>select

 聚合函数只能在分组之后使用

如果一张表没有写group by默认所有数据就是一组

 三、having

跟where是一模一样的,也是用来筛选数据的,但是having是跟在group by之后的。where是对整体数据做一个初步的筛选,而having是对分组之后的数据再进行一次针对性的筛选

 1.统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门

select post,avg(salary) from emp where age > 30 gruop by post;    #统计各部门年龄在30岁以上的员工平均工资
select post,avg(salary) from emp where age > 30 gruop by post having avg(salary) > 10000;

强调:having必须在group by 后面使用,如果having筛选条件是聚合函数,select查询的也要有聚合函数。

执行顺序:

from >> where >> group by >> having >> select

 四、distinct去重

 对重复的数据进行去重。去重的数据必须是一模一样的才能去重,只要有一个不一样,都不能算是重复的数据。

select distinct age from emp;

执行顺序:

from >> where >>group by >>having >>select >>distinct

五、order by  排序

默认是升序  asc

降序   desc

select * from emp order by salary;   #默认升序
select * from emp order by salary desc;   #降序排
select * from emp order by age desc,salary asc; #先按照age降序排,在age相同的情况下再按照salary升序排

统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序

select post,avg(salary) from emp where age >10 group by post  having avg(salary)>1000 order by avg(salary);

执行顺序:

from >> where >> group by >> having >> order by >>select

六、limit

限制展示数据的条数,一般用在网页的分页

select * from emp limit 5;   #只显示数据的五条

select * from emp limit 5,5;

当limit只有一个参数的时候,表示的是只展示几条

当limit有两个参数的时候,第一个参数表示的起始位置,第二个参数表示从起始位置开始往后展示的条数。

查询工资最高的人的详细信息(先按照薪资排序,再用limit限制只取一条)

select * from emp order by salary desc limit 1;

七、正则 regexp

select * from emp where name regexp '^j.*(n|y)$';

原文地址:https://www.cnblogs.com/wangcuican/p/11389959.html