查询语句

业务中最常见的执行操作就是查询

查询语句执行完的结果依然是一张新的表,结果集

一、基本查询语句

  • 一般查询
语法:表的 哪些列
select 字段1,字段2,字段3.. from 表名

示例:

select `stu_name`,`stu_age` from stu_info;

* 号表示所有字段
select * from stu_info ; //查询所有列
  • 条件查询

    语法:表,哪些行,哪些列
    select 字段1,字段2,..from 表名 where 条件
    

    示例:

    1. 查询所有的男生姓名
    select `stu_name` from stu_info where `stu_sex`=1 
    
    1. 查询所有男生的姓名和年龄
    select `stu_name`,`stu_age` from stu_info where `stu_sex`=1
    
    条件
    where 条件表达式
    变量    =>   字段名
    常量    =>   数值、字符串
    运算符  =>   参照以下表格
    
    运算符
    运算符 含义 用法表达式 查询结果
    = 全等比较 stu_sex = 1 查询所有的男生信息
    <>, != 不等于 stu_sex<>1 查询所有的非男生信息
    >,>=,<,<= 大小关系比较 stu_age>=20 查询所有的年龄在20及20岁以上的学生信息
    is null 判断为空 c_id is null 查询所有c_id项为NULL的数据行,(未分班学生)
    is not null 判断非空 c_id is not null 查询所有c_id项非NULL的数据行,(已分班学生)
    between ... and ... 值在某个区间 stu_age between 18 and 25 查询所有年龄在18到25岁之间的学生信息
    not 逻辑非 not c_id='c001' 查询除了3班以外学生信息
    or 逻辑或 c_id='c001' or c_id='c002' 查询c001和c002班学生的信息
    and 逻辑与 c_id='c003' and stu_sex=0 查询c003班所有女生信息
    like 模糊等于 stu_name like '张%' "字符%" 以指定字符开头
    "%字符" 以指定字符结尾
    "%字符%" 包含指定字符
    () 提升表达式优先级 stu_age > (18 + 2) 查询年龄大于20的学生信息
    练习
    1. 查询c003班和c002班,年龄在18-25周岁之间的张姓女同学 年龄,姓名 ,班级信息

      select 
             *
      from 
            stu_info 
      where 
           (`c_id`='c_003' or `c_id`='c_002')
      and
           (`stu_age` between 18 and  25)
      and
           (`stu_name` like '张%')
      and
           (`stu_sex`=0)
      
  1. 查询所有已分班的学生中,年龄小于18岁的男同学,基本信息

二、进阶查询

  • 排序查询

    语法: 查询结果集 +排序语句
    select 语句 order by 排序依据字段  asc / desc
    
    • 升序/降序

    查询c_003班学生信息,并按照年龄进行升序/降序

    select * from stu_info where `c_id`='c_003' order by  `stu_age` asc   //升
    select * from stu_info where `c_id`='c_003' order by  `stu_age` desc  //降
    

    排序语句永远写在查询语句之后,是查询语句的结果集进行操作,生成新的结果集

  • 限定查询 limit 起始数据下标, 截取数据长度

    对查询结果集进行分段截取

    select 基本查询语句  limit 结果集行标,数量
    

    示例:

    查询c_002班学生 年龄升序,取前两个数据

    select * from stu_info where 'c_id'='c_002' order by `stu_age` asc limit 0,2
    
  • 聚合函数(统计查询)

语法:对字段数据进行统计
select  聚合函数(字段名) as 新字段名  from 表 where 条件
  • 平均值 avg() 使用 as关键字对查询结果集字段进行重命名
select  avg(`stu_age`) as `avgage` from stu_info where `c_id`='c_003'
  • 数据列求和sum()
select sum(`stu_age`) as `totalAge` from stu_info where `c_id`='c_001'
  • 最大值 max()
select max(`stu_age`) as `maxAge` from stu_info ;
  • 最小值min()
select min(`stu_age`) as `minAge` from stu_info ;
  • 统计字段的数据总数 count(id)

获取c_003班学生人数

select count(`stu_id`) as `3班总人数` from stu_info where `c_id`='c_003'
  • 分组查询 group by

    select 聚合函数(统计的字段),分组的字段  from  表 where 条件 group by  分组的字段
    

    示例:

    查询c_003班,男生女生的人数

    分析,统计数量 count(统计字段:stu_id) ,分组 按stu_sex字段分组

    select 
       count(`stu_id`) as `stuCount`,`stu_sex` 
    from 
       stu_info 
    where 
       `c_id`='c_003'
    group by `stu_sex`
    

    查询每个班的最大年龄和最小年龄

    select  
        max(`stu_age`) as `最大年龄`,
        min(`stu_age`) as `最小年龄`,
        `c_id`         as `班级`
    from
        stu_info
    group by
        `c_id`
    
  • having子句

    语法: 分组查询语句 having 条件表达式

    是对分组查询的结果集,再次进行条件查询

    示例: 查询平均年龄小于25岁的班级

    select
       avg(`stu_age`) as `平均年龄`,
       `c_id`         as `班级`
    from 
       stu_info
    group by
       `c_id`
    having
       `平均年龄` < 25(结果集中的字段。与原数据表无关)
    

三. 子查询语句

查询对象不再是一张具体的表,而是另一条查询语句的结果集,子查询

语法
select  表名.字段  from (select 查询语句) as 表名  where  查询条件

示例:子查询写在 外层查询语句 “表” 的位置

select
  s_table.*
from
  (select `stu_id`,`stu_name`,`stu_age` from stu_info) as s_table
where
  s_table.age > 32

示例2:子查询语句写在where子句后面

查询已经分到当前开设课程的学生信息

select * from stu_info where  c_id = 'c_002' or c_id = 'c_001' or c_id="c_003" or c_id="c_004";

select stu_info.* from stu_info
where
stu_info.c_id 
in
(select `c_id` from stu_class)
  • 子查询书写位置, 表的位置
  • 写在where 字段 in / not in (子查询语句)
  • 子查询语句的列不能有重复列
  • 子查询语句写在表位置,一定要通过as 子查询结果集命名,外层查询必须使用该名称来查询相应字段
  • 子查询可以无限嵌套,结果依然是一个结果集,是一张新表
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容