5、【MySQL】单表查询

一、简介

单表查询语法:

select 字段1, 字段2, ... 
from table
where 条件 
group by field
having 筛选
order by field
limit   限制条数;

重点在于关键字的优先级:

from
where
group by
having
select
distict
order by
limit

所以,在执行单表查询的时候:

第一步:找到表,from
第二步:用where指定的约束条件,去表中取出相应的记录
第三步:将取出的记录进行分组group by,如果没有group by,则将整体作为一个整体
第四步:将分组的结果进行having过滤
第五步:执行select
第六步:去重
第七步:将结果按条件排序:order by
第八步:限制结果的显示条数

二、where约束

where子句中可以使用:

(1)比较云算法:>,<,>=,<=,==,!=
(2)between 80 and 100       # 值在80到100之间    
(3)in (80, 90, 100)     # 值时80 90 100中的一个
(4)like'z%' 或like 'zy_'     # %表示多个任意字符,_表示任意一个字符
(5)and or not               # 逻辑运算符,在多个条件可以之间使用逻辑运算符

1、创建表
create table employee(
    id int primary key 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
);
2、插入记录
insert into employee(name ,sex,age,hire_date,post,salary,office,depart_id) values
    # 部门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),
    ('xiaomage','male',48,'20101111','teacher',10000,401,1),
    # 部门2
    ('歪歪','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),
    # 部门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);

3、单条件查询
select id, name from employee where id < 5;
4、多条件查询
select name from employee where post = 'teacher' and salary > 100;
5、关键字查询between and
select name from employee where salary between 1000 and 2000;
6、关键字查询 in 集合查询
# where in
select name, salary from employee where salary =1000 and salary = 2000 and salary = 3000;
# 等价于:
select name, salary from employee where salary in (1000, 2000, 3000);
# where not in
select name,salary,post from employee where post='operation' and (salary not in(10000.13));
7、模糊查询

(1)通配符%,任意多个字符

select * from employee where name like  "陈咬%"

(2)通配符_,任意单字符

selelct * from employee where name like "ale_"

三、分组查询 group by

分组查询时发生在where之后,即分组时基于where之后得到的记录而进行的。

分组:将所有记录按照某个相同的字段进行归类,比如针对员工信息表的职位分组,或者按照性别分组等。

可以按照任意字段进行分组,但是分组完毕之后,比如group by post,只能看post字段,如果想要看组内信息,需要借助聚合函数。

使用分组查询,需要设置查询模式:only_full_group_by,如果没设置,查询结果默认是组内的第一条记录,没有任何意义。设置语句如下:

set global sql_mode='ONLY_FULL_GROUP_BY';

查看sql_mode的方式如下:

select @@global.sql_mode;

(1)根据部门进行分组

select post from employee group by post;

group by之后只能查看当前字段,如果想看组内信息,需要借助聚合函数。

四、聚合函数

常见的聚合函数有:

max()               # 求最大值
min()               # 求最小值
avg()               # 求平均值
sum()               # 求和
count()             # 求总个数
group_concat(字段名) # 拼接

强调:聚合函数聚合的是组内内容,如是没有分组,则默认是一组
示例

# 查每个部门有多少个员工
select post, count(*) from employee group by post;

# 查每个部分的平均薪水
select past, avg(salary) from employee group by  post;

# 查岗位名以及岗位包含的所有员工名字
select post, group_concat(name) as name from employee group by post;  #as name:重命名的字段名

# 查公司内部男女员工的个数
select sex, count(id) from employee group by sex;

# 查岗位名称以及各个岗位的最高薪资
select past, max(salary) from employee group by post;

# 查男员工于男员工的平均薪资,女员工和女员工的平均薪资
select sex, avg(salary) from employee group by sex;

五、having过滤

优先级:
    1、执行优先级从高到低:where > group by > having
    2、where发生在分组group by之前,因而where中可以有任意字段,但是绝对不能使用聚合函数
    3、having发生在分组group by之后,因而having中可以使用分组的字段,无法直接取到其它字段,可使用聚合函数
示例

# 查询所有salary大于10000的员工信息
select * from employee having salary > 10000;

# 查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数
select post,group_concat(name),count(1) from employee group by post having count(id)<2;
# group_concat(name): 将每个部门的员工姓名进行拼接,作为一个新字段展示
 
# 查询各岗位平均薪资大于10000的岗位名、平均工资
select post,avg(salary) from employee group by post having avg(salary)>10000;
 
# 查询各岗位平均薪资大于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;        # 默认升序,根据age升序
select * from employee order by age asc;      # asc 升序
select * from employee order by age desc;      # desc 降序

按多列排序:

# 先按照age升序排序,如果年纪相同,则按照id降序
select * from employee order by age asc, id desc;

七、limit限制查询的记录数

# 查询前三行记录
select * from employee limit 3;
 
# 从第0开始,即先查询出第一条,然后包含这一条在内往后查5条
select * from employee order by salary desc limit 0, 5;
 
# 从第5开始,即先查询出第6条,然后包含这一条在内往后查5条
select * from employee order by salary desc limit 5, 5;

你可能感兴趣的:(5、【MySQL】单表查询)