zoukankan      html  css  js  c++  java
  • 小贝_mysql select5种子句介绍

    mysql select5种子句介绍

    简要
    一、五种字句
    二、具体解释五种字句

    一、五种字句

    where、group by、having、order by、limit


    二、具体解释五种字句

    2.1、理解where子句

    理解select 列1。列2… fromtable where where子句

    a、 把where子句看成表达式。到table中查询满足表达式成立的行

    b、 上述查询语句的运行顺序。先找到表table后。再运行where子句。满足表达式后进行展示。

    c、比較运算符和逻辑运算符

    2.2、groupby

    2.2.1、理解分组作用,适用于统计

    2.2.2、与聚合函数一起适用。

    max(列)。min(列)。avg(),sum(),count()

    2.2.3、依照分组后。仅仅能得到每一个分组的第一条数据

    2.2.4、案例 table表以及数据例如以下:

    typeid

    name

    price

    1

    A1

    10

    2

    B1

    11

    1

    A2

    12

    2

    B2

    22

    具体解释1: select typeid,max(price) from table group by typeid

    2.2.4.1、从table表中,依照typeid进行分组得到

    typeid为1的数据

    typeid

    name

    price

    1

    A1

    10

    1

    A2

    12

    typeid为2的数据

    typeid

    name

    price

    2

    B1

    11

    2

    B2

    22

    2.2.4.2、从分组得到的数据中,依照聚合函数获取各个分组特定列的值

    typeid

    price

    1

    12

    2

    22

     

    具体解释2: select typeid, name,max(price) from table group by typeid

    依照具体解释1的运行流程得到结果

    typeid

    name

    price

    1

    A1

    12

    2

    B1

    22

    备注: 在对照数据是否正确时,发现name有误。

    由于price最大值为12时。name为A2而不是A1。

    这是由于。在分组得到的结果中,利用聚合函数max进行比較获取最大值时,是从第一行往下比較,第一行的name为A1,往下进行比較替换的仅仅是price值。

    因此。name仍然为第一行所得到的值。

     

    2.3、having

    having与where异同点:

    1、having与where类似,可筛选数据;where后的表达式怎么写,having就怎么写

    2、where针对表中的列发挥作用,查询数据。having针对查询结果中的列发挥作用,筛选数据

    案例: 查找两门及两门以上不及格的同学的平均分

    selectname,avg(score),sum(score<60) as s from stu group by name having s>=2;

     

    2.4、orderby 与limit

    1、order by 排序功能: 依照一个或多个字段对查询结果进行升序or降序排序.默认是升序

    语法: select 字段1,字段2from table order by 字段1 [asc|desc]

    2、limit 对查询结果取相应的条数

    语法: select 字段1,字段2from table limit offset,n

    (当中,0<=offset<总结果条数。n>0. offset为偏移量,n是指在offset的位置上取n条数据)

     

    2.5、总结

    1、语法顺序:

    select 字段1,字段2….. from table where子句 group by 字段 having 子句 order by 字段 [asc|desc] limit n,m;

    2、运行顺序

    2.1、from table 找到源数据

    2.2、where子句,把其看成一个表达式。在2.1的基础上一行一行推断表达式是否成立

    2.3、group by 在2.2的基础上,对字段进行分组,得到查询结果

    2.4、having 在2.3的基础上。对查询结果也是进行一行一行推断表达式是否成立。

    2.5、order by 在2.4的基础上。对查询结果进行排序

    2.6、limit 在2.5的基础上,取相应条数

    2.7、select 在2.6的基础上。对结果进行展示


    The quieter you become,the more you are able to hear。


  • 相关阅读:
    Leetcode Binary Tree Preorder Traversal
    Leetcode Minimum Depth of Binary Tree
    Leetcode 148. Sort List
    Leetcode 61. Rotate List
    Leetcode 86. Partition List
    Leetcode 21. Merge Two Sorted Lists
    Leetcode 143. Reorder List
    J2EE项目应用开发过程中的易错点
    JNDI初认识
    奔腾的代码
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6919662.html
Copyright © 2011-2022 走看看