zoukankan      html  css  js  c++  java
  • day037 mysql之单表查询

    一、单表查询语法

    1 select distinct  字段1,字段2,... from  库名.表名  
    2                                               where  条件
    3                                                group by  字段
    4                                                having   筛选条件
    5                                                order by 字段
    6                                                limit   限制显示条数

    二、关键字执行优先级

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

    执行步骤:

    1、from 找到表

    2、 拿着where指定的约束条件,去表中取一条条记录

    3、将取出的记录按group by 分组,如果没有group by 则整体为一组

    4、将分组结束进行having条件筛选

    5、执行select

    6、distinct 去重

    7、将结果按order by 排序

    8、limit限制显示条数

    三、简单查询

    select * from employee;  #查询表employee 中所有信息
    
    select name,salary from employee;  #查询表employee 中姓名,工资
    
    #去重
    select distinct post from employee;   #查询post字段信息并去重
    
    #通过四则运算查询
    select  salary*12  from employee;   #查询工资乘以12
    
    #自定义显示格式
    select concat("姓名: ",name,"  年薪: ", salary*12) as gongzibiao from employee;     #  表头和数据都是自己定义的格式 
           #concat()用于连接字符串   concat_ws() 将第一个参数作为分隔符进行字符串拼接

    四、where 条件

    where 不能使用聚合函数

    where语句可以使用的有;

    1、比较运算符: = >  <  >=   <=  !=

    2、逻辑运算 and     or      not   ,在多个条件中可以直接使用

    3、between  a   and  b

    4、in(x,  y,  z)

    5、like 模糊匹配  %表示匹配一个字符,_表示匹配一个字符

    #单条件查询
    select name from employee where post ='sale';
    
    #多条件查询
    select  name from  employee where post='sale'  and  asalary >1000;
    
    #between and 关键字
    select name from employee where salary between 1000 and 20000;
    
    #  is 关键字 (用于判断是否为null)
    select post from employee where post_comment is NULL;
    
    
    # in  关键字
    select name from employee where salary in(3000,4000,8000);
    
    #like 关键字   
    select * from employee where name like"al%" ;  #  %表示匹配所有字符
    select * from employee where name like"al_" ;   #  _表示匹配任意一个字符

    五、分组group by

    大前提:

        分组发生字where之后,即分组是基于where之后得到的记录而进行的。

        可以按照任意字段分组,但是分组完毕后,比如group by post,只能查看post字段,如果想查看

    组内信息,需要借助于聚合函数。

    聚合函数:

    聚合函数聚合的是组的内容,如没有分组,则默认是一组

    group_concat()  #字符串拼接
    select group_concat(name)  from meployee  group by post;
    
    count()   #统计个数
    select count(*) from employee;
    
    max()    #最大值
    select max(salary)  from employee ;
    
    min()      #最小值
    select min(salary)  from employee;
    
    avg()      #平均数
    select avg(salary)  from employee;
    
    sum()     #总数
    select sum(salary)  from employee;

    练习:

    #统计各部门年龄在30岁以上员工的平均工资

    select post ,avg(salary) from employee where age>=30 group by post

    六、having 过滤

    having的语法格式和where是一模一样的,只不过having是在分组之后进行的进一步的过滤,where不能使用聚合函数,having是可以使用聚合函数的
    #!!!执行优先级从高到低:where > group by > having #1. Where 发生在分组group by之前,因而Where中可以有任意字段,但是绝对不能使用聚合函数。 #2. Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,having是可以使用聚合函数


    #练习:

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

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

    七、order by 排序

    select  *  from   employee   order by   salary;           #按工资排序  默认是升序

    select  *  from   employee   order by   salary asc;     # asc 表示升序

    select  *  from   employee   order by   salary desc;   # desc 表示降序

    select  *  from   employee   order by   age, salary desc    #先按年龄升序 再按工资降序排列

    八、limit 限制记录条数 

    可以应用于分页显示

    # 练习

    select  *  from   employee   order by  salary desc  limit 3;     #取三条数据

    select   *  from  employee  order by   salary desc  limit 0,5 ;  #从索引0开始,取五条数据,默认第一条数据索引是0

    select   *  from  employee  order by   salary desc  limit 5,5 ;   #从索引5开始,取五条数据

    九、使用正则表达式

    格式:

    select  * from  employee where name regexp 正则表达式;

    # 练习

    select * from employee where name regexp ' ^jin.*[g|n]$ ' ;  #查询所有员工中名字是jin开头,n或g结尾的员工信息

    ####

    对字符串的匹配方式:

    1、精确匹配     where name ='dasab' ;     

    2、模糊匹配    where name like 'da%' ;

    3、正则表达式   where name regexp 'b$' ;  

  • 相关阅读:
    Jquery 获取CheckBoxList 中选择的值和获取RadioButton 选中的状态
    JS数组取相同的值和不同的值
    JS删除数组总是报错
    调用WebAPI接口
    MVC3 Excel导入数据到sql Server数据库
    MVC JS动态添加 @Html.DropDownList
    得瑟一下
    出差小记
    jQuery自学打卡二
    jQuery自学打卡一
  • 原文地址:https://www.cnblogs.com/zhang-yl/p/10085186.html
Copyright © 2011-2022 走看看