zoukankan      html  css  js  c++  java
  • 单表查询,多表查询

    单表查询

      表的基本查询语句

        from
        where
        group by
        having
        distinct
        order by
        limit

      ​​select * from empG; 
      当表字段特别多的时候 结果的排版可能会出现混乱的现象 
      你可以在查询语句加G来规范查询结果

      单表查询的前期准备

      
    ​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)
    ;
    # ps:
    ​如果在windows系统中,插入中文字符,select的结果为空白,
    ​可以将所有字符编码统一设置成gbk
    插入记录

      1、语法执行顺序

        # 初识查询语句
        select id,name from emp where id >= 3 and id <= 6;​  ① 
        
    # 先后顺序
        from
        where
        group by
        having
        distinct
        order by
        limit

      2. where约束条件

      
    # 1. 查询ID大于等于3 小于等于6的数据
    select * from emp where id >= 3 and id <= 6;
    select * 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 (2000,18000,17000);​​  ③
    
    # 3. 查询员工姓名中包含o字幕的员工姓名和薪资​
    # 刚接触MySQL查询的时候,建议按照查询的优先顺序,拼写出sql语句​
    '''
    先是查哪张表 from emp
    再是根据什么条件去查 where name like %o%
    再是对查询出来的数据筛选展示部分 select name, salary​​
    ​​'''​
    select name, ​salary from emp where name like '%o%';  ④
    
    # 4. 查询员工信命是由四个字符组成的员工姓名和其薪资
    select name,salary from emp where name liko '____';​  ⑤ # 四个下划线 表示模糊匹配​
    select name,salary from emp where char_length(name) = 4; ​ ⑤
    
    # 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)​
    slect name,post from emp where post_comment = null;  # 报错
    select name,post from emp where post_comment is null;  ⑧
    View Code

      3. group by(分组)

      
    数据分组的应用场景:每个部门的平均薪资,男女比例等
    ​
    # 1. 按部门分组​
    select * from emp group by post;  ⑨ # 分组后取出的是每一个组的第一条数据
    select id,name,sex from emp group by post;  ⑨ # 验证​​
    ​注意:
    ​设置sql_mode 为only_full_group_by,
    ​意味着以后但凡分组,只能取到分组的依据(即组名,如图⑩只有组名一列)
    ​
    不应该再去取里边的单个元素的值(如图⑨),那样的话分组就没有意义了,
    ​因为不分组就是对单个元素的随意取值​
    ​
    设置命令:
    set global sql_mode="strict_trans_tables,only_full_group_by";​
    ​​​设置后需要重新连接客户端:
    select * from emp group by post;  # 报错
    select id,name,sex from emp group by post​​;  # 报错
    select post from emp group by post;  # 获取到部门信息⑩​
    强调:
    只要分组了,就不能够再“直接”​​查找到单个数据信息了,只能获取到组名
    ​
    ​
    # 2. 获取每个部门的最高工资
    聚合函数:max, min, avg, sum, count
    ​​# 以组为单位 统计组内数据>>>聚合查询(聚集到一起合成为一个结果)
    # 每个部门的最高工资
    select post​​,max(salary) from emp group by post;  ⑩①
    # 每个部门最低工资
    select post,max(salary) from emp group by post;  ⑩①
    # 每个部门的平均工资
    select post avg(salary) from emp group by post;  ⑩① 
    # 每个部门的工资总和
    select post,sum(salary) from emp group by post;  ⑩① 
    # 每个部门的人数
    select post count(id) from emp group by post;  ⑩① 
    ​​​​​​​​​
    ​​
    # 3. 查询分组之后的部门名称,和每个部门下所有的学生姓名​
    # gruop_concat()​ 分组之后用的,
    ​不仅可以用老显示除分组外字段,还有拼接的作用
    select post,name from emp group by post;  ⑩②# 报错
    select post,group_concat(name) from emp group by post;   ⑩②# 正常显示
    # 拼接
    select post,group_cancat​​​​(name,'_SB') from emp group by post;  ⑩③
    select post,group_cancat(name,'',salary) from emp group by post;  ⑩③
    # 起别名​
    select post,group_cancat(salary) as details from emp group by post;​​​  ⑩④
    ​
    # 4. 补充concat() 不分组的时候可以用,拼接字符串达到好看的效果
    ​as 语法  取别名
    select name concat("NAME:",name) as 姓名, concat('SAL:'salary) as 薪资 from emp;​​​​  ⑩⑤
    
    # as语法,既可以给字段起别名,也可以给表起别名
    ​​​select emp.id,emp,name from emp as t1;  ⑩⑥# 报错,因为表已经被改名为 t1了
    select t1.id, t1.name​ from emp as t1;  ⑩⑥# 成功
    
    # 四则运算
    # 查询每个人的年薪
    select name,salary*12 as annual_salary from emp​​​​;
    select name,salary*12 annual_salary from emp;  ⑩⑦# as可以省略​
    ​​
    View Code

      4. having(在分组之后进行的过滤)

      
    截至目前已经学习到的语法
    select 查询字段1,查询字段2,...​ from 表名
            where 过滤条件
            group by 分组依据
    ​
    # 语法虽然这么写,但是执行顺序确不一样​​​,为:
    from 
    where
    group by
    select​​​​​
    ​
    having​​的语法格式与where一致,只不过having是在分组之后进行的过滤
    即where虽然不能用聚合函数,但是having可以!
    ​
    1. 统计各部门年龄在30岁以上的员工平均工资
    并且保留平均工资大于10000的部门​
    select ​post,avg(salary) from emp
            where age >=30
            group by post
            having avg(salary)​​​ > 10000;  ⑩⑧
    ​
    强调:having 必须在 group by​​后面使用
    select * from emp having avg(salary) > 10000;  ⑩⑧# 报错​
    ​​​​
    View Code

      5. distinct(去重)

      
    # 对有重复的展示数据,进行去重
    select distinct post from emp;​  ⑩⑨
    View Code

      6. order by(排序)

      
    select * from emp order by salary asc;  ②⑩# 默认不写 就是升序的
    select * from emp order by salary desc;  # 降序
    
    select * from emp order by age desc;  # 降序
    
    # 先按照age降序,在年纪相同的项之间 按照薪资升序排
    select * from emp order by age desc,salary asc;​​​​​  ②⑩①
    
    # 统计各部门年龄在10岁以上的员工平均工资,
    ​并且保留平均工资大于1000的部门,然后对平均工资排序​​
    ​select post,avg(salary) from emp
            where age >10
             group by post
             having avg(salary) > 1000
             order by avg(salary);​​​​  ②⑩②
    ​​
    View Code

      7. limit 限制展示条数

      
    # 限制展示条数
    select * from emp limit 3;  ②⑩③
    # 查询工资最高的人的详细信息
    select * from emp order by salary desc limit 1;  ②⑩③
    
    # 分页显示
    select * from emp limit 0,5;  ②⑩④
    ​# 第一个参数是起始位置,第二个参数是的是条数,不是索引位置
    select * from emp limit 5,5;​​​​​​​  ②⑩④
    View Code

      8. 正则(regexp)

      
    select * from emp where name regexp '^j.*(n|y)$';  
    ​# 匹配表中所有记录 符合(j开头 n或y结尾的字符)
    View Code

    多表查询

      前期准备(建表和插入数据)②⑩⑤

        当初为什么我们要分表,就是为了方便管理,在硬盘上确实是多张表,
        ​但是到了内存中我们应该把他们再拼成一张表进行查询才合理
      
    # 建表
    create table dep(
    id int,
    name varchar(20) 
    );
    create table emp(
    id int primary key auto_increment,
    name varchar(20),
    sex enum('male','female') not null default 'male',
    age int,
    dep_id int
    );
    ​
    # 插入数据
    insert into dep values
    (200,'技术'),
    (201,'人力资源'),
    (202,'销售'),
    (203,'运营');
    insert into emp(name,sex,age,dep_id) values
    ('jason','male',18,200),
    ('egon','female',48,201),
    ('kevin','male',38,201),
    ('nick','female',28,202),
    ('owen','male',18,200),
    ('jerry','female',18,204)
    ;
    ​
    View Code

      表查询

      
    select * from emp,dep;  ②⑩⑥# 左表一条记录与右表一条记录都对应一遍>>>笛卡尔积
    
    # 将所有的数据都对应一遍,虽然不合理,但是其中有合理的数据,
    现在我们需要做的 就是找出合理的数据​​​
    
    # 查询员工及所在部门的信息
    select * from emp,dep where emp.dep_id =dep.id​​​;  ②⑩⑦
    ​# 可以对照上边的笛卡尔积的图  容易理解
    # 查询部门为技术部的员工及部门信息
    select * from emp,dep where emp.dep_id = dep.id and dep.name = '技术';​​  ②⑩⑦
    
    
    # 将两张表关联到一起的操作,有专门的对应的方法:内连接、左连接、右连接、全连接# 1. 内连接:只取两张表有对应关系的记录
    select * from emp inner join dep on emp.dep_id = dep.id;  ②⑩⑧
    select * from emp inner join dep on emp.emp_id = dep.id where dep.name = '技术';​​  ②⑩⑧
    
    # 2. 左连接:在内连接的基础上,保留左表没有对应关系的记录(没有的字段用null替代)
    select * from emp left join dep on emp.dep_id = dep.id;  ②⑩⑨
    
    # 3. 右连接:在内连接的基础上,保留右表没有对应关系的记录
    select * from emp right join ​​​​​​dep on emp.dep_id = dep.id;  ②⑩⑨
    
    # 4. 全连接:在内连接的基础上,保留左右表没有对应关系的记录​​
    ​​​​select * from emp left join dep on emp.dep_id = dep.id
    union
    select * from ​​emp right join dep on emp.dep_id =dep.id;  ③⑩
    View Code

      子查询

      
    # 就是将一个查询语句的结果用括号括起来,当做另一个语句的条件去用
    # 1. 查询部门是技术部或者人力资源部的员工信息​
    思路:​
    先获取技术部和人力资源部的id号,(这个是部门的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;​​
    ​​
    # 可以给表起别名
    # 可以给查询出来的虚拟表起别名
    # 可以给字段起别名
    View Code

    SQL命令执行结果

      根据代码后边的编号,进入幕布对应查看即可
      去幕布>>>
  • 相关阅读:
    iptables详解(7):iptables扩展之udp扩展与icmp扩展
    iptables详解(6):iptables扩展匹配条件之’–tcp-flags’
    iptables(五)iptables匹配条件总结之二(常用扩展模块)
    Neutron之OVS
    Neutron三层网络服务实现原理
    Neutron二层网络服务实现原理
    LoadBalancerv2的原理分析
    Haproxy介绍
    基于zepto的手机焦点图touchstart touchmove
    zepto.js 处理Touch事件(实例)
  • 原文地址:https://www.cnblogs.com/buzaiyicheng/p/11391445.html
Copyright © 2011-2022 走看看