Python 数据行的高级查询

单表查询

一 以此表为例

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)
;
表建立

1.where约束条件查询

1.语法书写顺序和执行顺序

书写顺序
    select 
    from
    where


执行顺序
    from  # 确定到底是哪张表
    where  # 根据过来条件 筛选数据
    select  # 拿出筛选出来的数据中的某些字段
View Code

2.常见的where条件查询语句

# 1.between and: 闭区间 ;
例:查询id大于等于3小于等于6的数据
select id,name from emp where id >= 3 and id <= 6;
select *  from emp where id between 3 and 6; 
#补充:!= : 不等与
      >= <= :大于 等于

# 2.in: 在某一个集合中
例:查询薪资是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.模糊匹配 like(%:匹配多个任意字符;_:匹配一个任意字符)
例:查询员工姓名中包含o字母的员工姓名和薪资
# 在你刚开始接触mysql查询的时候,建议你按照查询的优先级顺序拼写出你的sql语句
"""
先是查哪张表 from emp
再是根据什么条件去查 where name like ‘%o%’
再是对查询出来的数据筛选展示部分 select name,salary
"""
select name,salary from emp where name like '%o%';

例:查询员工姓名是由四个字符组成的员工姓名与其薪资
select name,salary from emp where name like '____';
select name,salary from emp where char_length(name) = 4;

# 4.not 取反
例:查询id小于3或者大于6的数据
select *  from emp where id not between 3 and 6;

例:查询薪资不在20000,18000,17000范围的数据
select * from emp where salary not in (20000,18000,17000);

# 5.针对null不能用等号,只能用is
例:查询岗位描述为空的员工名与岗位名  针对null不能用等号,只能用is
select name,post from emp where post_comment = NULL;  # 查询为空!
select name,post from emp where post_comment is NULL;
select name,post from emp where post_comment is not NULL;
View Code

2.group by 分组 和 聚合函数及having

1.语法书写顺序和执行顺序

书写顺序
    select 
    from
    where
    group by

执行顺序
    from
    where
    group by
    select
View Code

2.注意:

#1.分组之后应该做到最小单位是组,而不应该再展示组内的单个数据信息

#2.MySQL中分组之后 只能拿到分组的字段信息 无法直接获取其他字段信息,但是你可以通过其他方法(聚合函数)简介的获取。如果你的MySQL不报错 说明严格模式没有设置。设置方法如下

show variables like '%mode%';
#set session  当前窗口有效
#set global  全局有效 
set global sql_mode="strict_trans_tables,only_full_group_by";
View Code

#3.聚合函数 max min sum count avg只能在分组之后使用;如果一张表没有写group by默认所有的数据就是一组。

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

3.常见的分组使用,以上表为例

# 1.获取每个部门的最高工资     聚合函数 max min avg sum count
  select post,max(salary) from emp group by post;
补充:给字段取别名
select post as '部门',max(salary) as '最高工资' from emp group by post;
select post '部门',max(salary) '最高工资' from emp group by post;

# 2.每个部门的最低工资
select post,min(salary) from emp group by post;

# 3.每个部门的平均工资
select post,avg(salary) from emp group by post;

# 4.每个部门的工资总和
select post,sum(salary) from emp group by post;

#5. 每个部门的人数
select post,count(id) from emp group by post;
补充:在统计分组内个数的时候 填写任意非空字段都可以完成计数,推荐使用能够唯一标识数据的字段比如id字段
分组聚合函数
# 查询分组之后的部门名称和每个部门下所有的学生姓名
# group_concat(分组之后用)不仅可以用来显示除分组外字段还有拼接字符串的作用
select post,group_concat(name) from emp group by post;

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

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

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


# 4.补充concat(不分组时用)拼接字符串达到更好的显示效果 as语法使用
select name as 姓名,salary as 薪资 from emp;
select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪资 from emp;

小技巧:
        concat就是用来帮你拼接数据
        concat 不分组情况下使用,内容拼接
        group_concat  分组之后使用,内容拼接,间接访问分组后其他字段的值
内容拼接

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


select post,avg(salary) from emp where age > 30 group by post having avg(salary) > 10000;
select post,avg(salary) from emp where age > 30 group by post where avg(salary) > 10000;  # 报错
having

3.distinct:去重

# 对有重复的展示数据进行去重操作
select distinct post from emp;
"""
    去重必须数据是一模一样的才能去重
    只要有一个不一样 都不能算是的重复的数据
"""

执行顺序
    from 
    where
    group by
    having
    select
    distinct
去重

4.order by排序

order by  排序
        默认是升序 asc
        也可以变成降序  desc



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

5. limit限制

    """
    当limit只有一个参数的时候  表示的是只展示几条
    当limit有两个参数的时候   第一个参数表示的起始位置 ,第二个参数表示从起始位置开始往后展示的条数
        第一条数据的索引为0
    
    """

select * from emp limit 5;  # 只展示数据的五条
select * from emp limit 5,5;  # 从索引5开始,向后展示5条数据

分页核心SQL:
select * from t3 limit (page-1)*offset, offset;
限制

6.regex 正则

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

多表查询

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

#ps:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk
表建立

1.表查询分为两大类:联表查询,子查询。

2. 将两张表关联到一起的操作,有专门对应的方法:

#1。内连接:只取两张表有对应关系的记录。

#2.左连接: 在内连接的基础上保留左表没有对应关系的记录

#3.右连接: 在内连接的基础上保留右表没有对应关系的记录

#4.全连接:在内连接的基础上保留左、右面表没有对应关系的的记录

3.操作语法

# 1、内连接:只取两张表有对应关系的记录
select * from emp inner join dep on emp.dep_id = dep.id;
select * from emp inner join dep on emp.dep_id = dep.id
                            where dep.name = "技术";
内连接

# 2、左连接: 在内连接的基础上保留左表没有对应关系的记录
select * from emp left join dep on emp.dep_id = dep.id;
左连接

# 3、右连接: 在内连接的基础上保留右表没有对应关系的记录
select * from emp1 right join dep on emp1.dep_id = dep.id;
右连接

# 4、全连接:在内连接的基础上保留左、右面表没有对应关系的的记录
select * from emp1 left join dep on emp1.dep_id = dep.id
union
select * from emp1 right join dep on emp1.dep_id = dep.id;
全连接

 

子查询

# 就是将一个查询语句的结果用括号括起来当作另外一个查询语句的条件去用
# 1.查询部门是技术或者人力资源的员工信息
"""
先获取技术部和人力资源部的id号,再去员工表里面根据前面的id筛选出符合要求的员工信息
"""
select * from emp where dep_id in (select id from dep where name = "技术" or name = "人力资源");

# 2.每个部门最新入职的员工 思路:先查每个部门最新入职的员工,再按部门对应上联表查询
select t1.id,t1.name,t1.hire_date,t1.post,t2.* from emp as t1
inner join
(select post,max(hire_date) as max_date from emp group by post) as t2
on t1.post = t2.post
where t1.hire_date = t2.max_date
;

"""
记住一个规律,表的查询结果可以作为其他表的查询条件,也可以通过其别名的方式把它作为一张虚拟表去跟其他表做关联查询
"""

select * from emp inner join dep on emp.dep_id = dep.id;
子查询

 

猜你喜欢

转载自www.cnblogs.com/tfzz/p/11391147.html