MySQL 一周目五日

单表数据查询

 1  筛选  where

方法  where 筛选条件

# 查询id在3到6的数据
  select * from 表名 where id >=3 and id <=6;
  select * from 表名 where id between 3 and 6;
# 查询薪资是20000、18000和17000的数据
    select  * from 表名 where salary=20000 or salary=18000 or salary=17000;
    select * from 表名 where salary in (20000,18000,17000);
两者书写格式不同,但是查找的内容是相同的

#查询薪资不在20000,18000和17000范围中的数据
select name,salary from emp where salary not in (20000,18000,17000); 采用成员运算的方法 #查询岗位描述为空的员工名与岗位名 select name,post from emp where post_comment is null; null的值不能用=,否则查询结果为空,只能用is

2  模糊查询  like

方法  like  

带有模糊查询关键字符的内容

关键字符  1  %  匹配任意个数的任意字符

关键字符  2  _   匹配单个个数的任意字符

如 包含o字母的名字 ——  ‘%o%’  第二个字符为o的三字词语 ——  ‘_o_’

# 查询员工姓名中包含o字母的员工姓名和薪资
   select name,salary from emp where name like '%o%';
# 查询员工姓名有四个字符组成的员工姓名和薪资
  select name,salary from emp where name like '____';
        模糊查询可以匹配任意字符的四个字符词语
  select name,salary from emp where char_lenth(name)=4;
        查询name的字符串长度为4的值
        %s_length 计算%s字符长度的内置函数

 3  分组  group by

某些具有相同特性的事物归为一类,看做是一个整体

 方法  group by 字段名  用字段名来进行分组

#根据性别进行分类
  select gender from 表名 group by gender;

注:分组之后默认只可以直接获取到分组的依据 无法再获取内部单个个体数据如果想要获取需要借助于一些方法

由于mysql5.6版本没有开启严格模式,因此书写select * from 表名的时候不会报错,5.7及以后的版本都会报错

开启严格模式

set global sql_mode="strict_trans_tables,only_full_group_by";

4  聚合函数

  4.1  最大值  max  

1.获取每个部门的最高工资   运用方式  select post,max(salary) from emp group by post;

  4.2  最小值  min

2.获取每个部门的最低薪资  运用方式  select post,min(salary) from emp group by post;

  4.3  平均值  avg

3.获取每个部门的平均薪资  运用方式  select post,avg(salary) from emp group by post;

  4.4  求和     sum

4.获取每个部门的薪资总和  运用方式  select post,sum(salary) from emp group by post;

  4.5  计数     count

5.获取每个部门的员工人数  运用方式  select post,count(id) from emp group by post;

5  过滤  having

  5.1  having与where比较

having与where功能本质上都是筛选,只是where用于分组前,having用于分组后

  5.2  过滤的应用案例

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

步骤1    搭建框架    统计部门平均工资,按部门进行分组
    select post,avg(salary) from emp group by post
步骤2    插入要求    统计年龄30以上员工,分组的对象前提是30以上
    在分组group by前插入    where age > 30
步骤三    得出10000以上的部门,对数据过滤
    在分组后添加    having avg(salary)> 10000
完整结果
    select post,avg(salary) from emp 
        where age > 30
        group by post
        having avg(salary) > 10000;

6  去重  distinct

  去重的前提必须是完全相同  

  select distinct 字段名 from 表名;  去重结果为非字段名中非相同项

  select distinct 字段名1,字段名2 from 表名;  去重结果为字段名1和字段名2叠加后不相同的项,更多字段名以此类推

7  排序  order by

  7.1  升序方法  select * from emp order by salary;  select * from emp order by salary asc;  不写默认为升序,升序关键字  asc

  7.2  降序方法  select * from emp order by salary desc;  降序关键字  desc

  7.3  如果数字相同,有并列,可以添加次选排序

    方法  如根据年龄升序,薪水降序排序  select * from emp order by age asc,salary desc;  其中年龄在前,为主排序,薪水在后为次排序,年龄相同的项,按薪水排序

8  分页  limit

  limit 5,5  limit后面接两位数,前者表示起始位置,后者表示展示条数。起始位置可以不写,默认从0开始,即变成  limit 5

  完整结构默认0  select * from emp limit 5;

  完整结构有起始数5  select * from emp limit 5,5;

9  正则  regexp

  9.1  正则定义

    使用一些特殊符号的组合去字符串中筛选出符合条件的数据

  9.2  正则运用案列

    查询姓名是以字母j开头 n或者y结尾的数据

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

posted @ 2021-09-07 22:30  aa_wang  阅读(45)  评论(0)    收藏  举报