zoukankan      html  css  js  c++  java
  • 47、表的关键字使用

    一、MySQL中使用的关键字

      select:查询

      where:筛选

      group by:分组

      having:筛选

      distinct:

      order by:排序

      limit:限制

      regexp:

      like:模糊查询

    1.1、表的数据

      当表中的字段比较多,展示时会错乱的时候,可以使用G 进行分行显示

      select * from empG

    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), #以下是教学部
    ('tom','male',78,'20150302','teacher',1000000.31,401,1),
    ('kevin','male',81,'20130305','teacher',8300,401,1),
    ('tony','male',73,'20140701','teacher',3500,401,1),
    ('owen','male',28,'20121101','teacher',2100,401,1),
    ('jack','female',18,'20110211','teacher',9000,401,1),
    ('jenny','male',18,'19000301','teacher',30000,401,1),
    ('sank','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.2、关键字的执行顺序

      执行顺序:from》where》select

      书写顺序:select id,name from emp where id>3;

      书写顺序和执行顺序并不一致,可以先书写select * from,之后根据需求将*替换

    二、where 筛选条件

      作用:将问题数据进行筛选

    2.1、筛选范围性中间值,可以使用and进行连接,或者between  

    select id,name,age from emp where id>=3 and id<=6;
    select id,name from emp where id between 3 and 6;  两者等价,between可以搭配not使用,即not between  :不在中间的

    2.2、筛选多个单一数值,可以使用or进行连接,或者in

    select * from emp where salary=20000 or salary=18000 or salary=17000;
    select * from emp where salary in (20000,18000,17000);    in可以搭配not使用,即not in:不在里面的

    2.3、模糊查询  like

     while * like  搭配  %  匹配多个任意字符

                _  匹配单一任意字符

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

    2.4、长度限制:char_length()

      while  char_langth(*)=n;    在*里面筛选出长度等于n的选项

    select name,salary from emp where char_length(name) = 4;

    2.5、当前数据为空:is null

      当描述内容为空时,应该使用 is null而不是= null

    select name,post from emp where post_comment is NULL;

    三、group by分组

      select * from 表名 group by 组名

      分组之后最小单位是组(显示的是组名),而不是数据,按照上面分组之后只能拿到分组,其他数据无法直接获取,需要依靠聚合函数

    3.1、聚合函数

      最大值 max:将部门进行分组,求最大薪资max(salary),

    select post as '部门',max(salary) as '最高薪资' from emp group by post;   可以使用as重新命名,

      最小值min:将部门进行分组,求最小薪资min(salary)

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

      求和sum:将部门进行分组,求薪资的总和sum(salary)

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

      计数count:将部门记性分组,求部门人数count(id)

    select post,count(id) from emp group by post;  # 常用 符合逻辑

      平均值avg:将部门进行分组,求平均值avg(salary)

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

      显示组内指定数据group_concat(指定数据,拼接内容):将部门进行分组,显示部门下员工的姓名,并且还支持拼接

    select post,group_concat(name,':',salary) from emp group by post;    使用name和salary进行拼接,中间使用:隔开
    select concat('NAME:',name),concat('SAL:',salary) from emp;    不分组是直接使用concat

      起别名as:不仅可以给字段起别名,还可以给表起别名

    select t1.id,t1.name from emp as t1;   #先执行from,将emp的名字改为t1,查看t1的id和name

    3.2、分组注意

      书写顺序:while》group by

      执行顺序:while》group by

      书写时分为三步:1.先进行筛选:select * from emp where age>30;

              2.接着进行分组:select * from emp where age>30 group by post;

              3.最后将*更改:select post,avg(salary) from emp where age>30 group by post;

    四、having分组之后的筛选

      having的使用方法和while是一致的,但是having是while筛选,接着group by分组之后的筛选,having可以和聚合函数搭配使用

      先筛选出30岁以上的,再将部门进行分组,最后保留平均薪资打到10000以上的部门

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

    五、distinct去重

      将age进行去重,但是要注意不要加入id

    select distinct id,age from emp;     需要id和age同时相同,才能去重
    select distinct age from emp;

    六、order by 排序

      order by asc:升序(asc可以不写)

      order by desc:降序

    6.1、order by和order by desc的混用

      先进行age的升序,一旦相同时,使用salary的降序

    select * from emp order by age desc,salary asc;

    6.2、先筛选出10岁以上的员工,接着讲部门进行分组,在筛选出平均工资大于1000的部门,最后按照平均工资进行升序

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

    七、limit限制展示条数

      为避免数据量过大,因此进行分页处理  limit 起始位置,展示条数

    select * from emp limit 起始位置,展示条数;     

    八、正则.

      正则:regexp

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

    九、多表操作

       多表查询分为联表以及子查询

      联表(将两张表进行拼接后再查询):当需要查询的信息在两张表里面,就需要先将两张表进行连接

      子查询(一个一个往下查询):当查询的信息在两张表里面,使用两张表的id的连接关系,先通过要求为条件找到找到被连接表的id,再以被连接表的id为条件找到连接表里面的信息。

    9.1、联表

      将两个表进行关系,需要查看一张汇总表时,需要进行拼接

      涉及到多表操作的时候 一定要加上表的前缀

    select dep.name from emp inner join dep
            on emp.dep_id = dep.id
            group by dep.name
            having avg(age) > 25
            ;

      inner join:内连接   将emp的dep_id和dep的id进行连接,如果都有则显示

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

      left join:左连接  将emp的dep_id和dep的id进行连接,只要左边(emp)有的都会显示,右边没有则填null

    select * from emp left join dep on emp.dep_id = dep.id;

      right join:右连接  将emp的dep_id和dep的id进行连接,只要右边(dep)有的都会显示,左边没有则显示null

    select * from emp right join dep on emp.dep_id = dep.id;

      union:全连接  将左右两边进行连接,没有的则用null显示

    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;

    9.2、子查询

      子查询为分步解决问题的思路,使用一个查询语句的结果去当另一个查询语句的条件

      查询部门是技术部门或者人力资源部门的人员

      1.先通过筛选部门的得到员工的相关id

    select id from dep where name='技术' or name = '人力资源';

      2.接着通过id找到人员姓名

    select name from emp where dep_id in (200,201);

      3.将两个信息合并就是,找到部门的人员信息,只不过是将id做一个中转

    select * from emp where dep_id in (select id from dep where name='技术' or name = '人力资源');

    10、exists 只返回布尔值

      和外层语句搭配使用,当返回True时执行,当外层语句返回False时,不执行

    select * from emp where exists 
            (select id from dep where id>3);

      

  • 相关阅读:
    barabasilab-networkScience学习笔记2-图理论
    barabasilab-networkScience学习笔记1-网络科学简介
    windows下R语言在终端的运行
    远程打印服务器
    矩震级Mw与地震矩M0的换算关系
    关于地震科学台阵数据中心的仪器记录值介绍
    capjoint中的tel3核心代码teleseis3.f90
    Centos7安装
    matlab中hold on 和hold off功能的区别
    sac cut
  • 原文地址:https://www.cnblogs.com/jingpeng/p/12845890.html
Copyright © 2011-2022 走看看