• 单表查询基础内容


    where子句中可以使用
    1. 比较运算符:>、<、>=、<=、!=
    2. between 80 and 100 :值在80到100之间 :包括两边的
    3. in(80,90,100)值是80或90或100
    4. like 'xiao%'可以是%或者_, %代表任意多字符,_表示一个字符 
    x%, x开头
    %a% , 包括a
    %t t结尾
    5. 逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not
    分组 group by
    小窍门,每的后面字段是分组的字段 
    分组之后select 后面不能出现任何除了分组依据字段外的其他字段
    但是可以和聚合函数连用
     select group_concat(name),post,count(id) from employee group by post;
    简单练习题 39.100.47.247
    class 1 2 3 
    每个班级的人数
    select cls, count(name) group by cls;
    年龄大于20 的同学的信息显示2条
    select * from class where age>20 limit 2;
    年龄大于20 ,分数从高到低排序的同学的信息显示2条
    select * from class where age>20 order by score desc limit 2
     每个班级的平均成绩
    select cls,avg(score) from class group by cls;
    年龄小于18岁,且成绩大于70分的同学信息
    select * from class where age < 18 and score > 70;
    年龄小于20岁,且平均成绩大于70分的同学信息
    select name,avg(score) from class where age < 20 group by name having avg(score) > 70;
    选出成绩在70到90之间的同学信息并按年龄降序排列
    select * from class where score between 70 and 90 order by age desc;
    筛选出成绩不小于80分的信息。从第2条显示,显示3条。
    select * from class where score>=80 limit 1,3; 
    每个班级年龄大于20 的同学数量,及最高成绩。
    select cls, max(score), count(name) from class where age>20 group by cls;
    重点中的重点:关键字的执行优先级
    1 from
    2 where
    条件里面不能出现聚合函数
    分组之前筛选的条件
    3 group by
    4 聚合函数(max(id)等)
    5 having 一般和聚合函数连用
    分组之后筛选条件
    6 select 结果
    7 distinct 去重
    8 order by
    9 limit
  • 相关阅读:
    探究 encode 和 decode 的使用问题(Python)
    C语言结构体在内存中的存储情况探究------内存对齐
    文件基本操作 (C语言)
    利用Xamaria构建Android应用-公交发车信息屏
    ChakraCore ,Net托管编程
    Go并发与.Net TAP
    码农视角
    让isis支持高德地图
    Fedora Server 上配置 MariaDb 集群
    .Net Sokcet 异步编程
  • 原文地址:https://www.cnblogs.com/Darry-Ring/p/12141990.html
Copyright © 2020-2023  润新知