zoukankan      html  css  js  c++  java
  • from、where、group、with、having、order、union、limit 的使用

    顺序很重要

      每次看数据库的一些语法时,都很自然的略过那一大堆的规则,比如说线下面这段select的语法:

    select [field1,field2...] func_name
    from table1,table2,view1
    [left|right join table2 on condition_what]
    [where condition_where]
    [group by field1,field2...]
    [with rollup]
    [having condition_where]
    [order by field1 [desc|asc]]
    [limit offset,length]

      实际用的时候,除非经常使用,有些项顺序已经习以为常了,但是更多的时候,总会将顺序搞错,比如group

    from

      后面跟表名或者视图名,表示从该表或者视图中查询数据。

      from后面可以跟多个表名或者视图名,此时表示全连接,一般不推荐使用。

    mysql > select tb_one.id,tb_two.name,tb_three.addr from tb_one,tb_two,tb_three;
    

      

    where

      后面跟的是选择的条件,条件可以是比较运算符<、=<、>、>=、=、!=、like、is not NULL等,或者逻辑运算符 and or (between and)等。

    mysql> select * from cate where (id>10 and name='abc') or (addr is not null);
    mysql> select * from cate where name like '%abc_';
    mysql> select * from cate where create_time between '2018-01-01 00:00:00' and '2018-07-20 00:00:00';
    

      后面的条件可以是各种形式,包括一个in 

    mysql> select * from cate where id in (select id from demo);
    

      

    group by

      后面通常跟着的是列名或者表达式,可以根据一列进行分组,也可以对多列进行分组,多列分组的时候,只有当进行分组的所有列完全相同时,才认为是同一个组的。

      另外,group by子句通常和聚合函数一起使用,比如sum,avg,count,min,max这几个聚合函数作为select的查询结果。

      如果使用group by之后,一般不会在select后面查询的结果中指定列或者某个字段,因为这样是没有意义的,在分组时,只会统计该组的第一条数据。

    #按照id分组,统计每组的price总和
    mysql > select id,sum(price) from cate group by id;
    
    #按照price和type分组,两条记录,如果price和type都对应相同,则认为是一组;否则不是一组
    mysql > select count(*) from cate group by price,type;

       可以在分组后面加上with rollup,会在分组之后对每个组进行汇总。

    having

      having和where只有很小的区别,都代表条件筛选,但是在having子句中,可以使用聚合函数;而where中是不能使用聚合函数。

      having子句是在where子句和group by子句后面的,其实having的功能可以看着再次筛选,即,前面的查询结果,再用having中的条件在来筛选一次。

    mysql> select * from cate where id > 6 having id > 7;
    #先找出id>6的记录,然后再从这些记录中找出id>7的记录
    
    mysql> select sum(id),kind from cate where id !=0 group by kind having sum(id) > 8;
    #同样是上面的那个逻辑,从结果中在筛选一遍
    

      

    order by

      order by子句后面可以是一个列或者多列、表达式、正整数。正整数表示按结果表中该整数所指的那一列进行排序。

      排序默认的是升序(asc),可以显式指定排序规则,升序(asc),降序(desc)。注意空值被认为是最小值。

    mysql> select * from cate order by id desc;
    #按照id降序排列
    
    mysql> select * from cate order by id asc,price desc;
    #先将结果按照id升序排列,如果id相同,则按proce降序排列
    

      

    limit

      用来限制返回结果的行数,后面可以是一个数,也可以是两个数。

    mysql> select * from cate limit 5;  
    #返回5条记录
    
    mysql> select * from cate limit 3,5;  
    #第一条记录下标为0,所以返回的结果是从第4条开始的5条记录:4,5,6,7,8
    

      

    union

      可以连接多个select语句,然后将多个select的结果整合到一个表中,最终生成一个表,表的字段名称是以第一个select的字段名称为准。

      需要注意的是,每一个select语句选择的列,应具有相同的列数,并且每一列的类型要相同。并且mysql会自动从最终结果中去处重复行。

    mysql> select cid,cname from cate
        -> union
        -> select id,name from demo;
    

      

     

  • 相关阅读:
    【bzoj2115】[Wc2011] Xor DFS树+高斯消元求线性基
    【bzoj1778】[Usaco2010 Hol]Dotp 驱逐猪猡 矩阵乘法+概率dp+高斯消元
    【bzoj4184】shallot 线段树+高斯消元动态维护线性基
    【bzoj4347】[POI2016]Nim z utrudnieniem dp
    【bzoj3105】[cqoi2013]新Nim游戏 高斯消元求线性基
    【bzoj4004】[JLOI2015]装备购买 贪心+高斯消元求线性基
    【bzoj4128】Matrix 矩阵乘法+Hash+BSGS
    【bzoj4269】再见Xor 高斯消元求线性基
    【bzoj2460】[BeiJing2011]元素 贪心+高斯消元求线性基
    【bzoj3526】[Poi2014]Card 线段树区间合并
  • 原文地址:https://www.cnblogs.com/-beyond/p/8490020.html
Copyright © 2011-2022 走看看