顺序很重要

  每次看数据库的一些语法时,都很自然的略过那一大堆的规则,比如说线下面这段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后面可以跟多个表名或者视图名,此时表示全连接,一般不推荐使用。

  1. 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)等。

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

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

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

  

group by

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

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

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

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

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

having

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

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

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

  

order by

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

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

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

  

limit

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

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

  

union

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

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

  1. mysql> select cid,cname from cate
  2. -> union
  3. -> select id,name from demo;

  

 

from、where、group、with、having、order、union、limit 的使用的更多相关文章

  1. select的5中子句where,group by, havaing, order by, limit的使用顺序及实例

    -- 语法: SELECT select_list FROM table_name [ WHERE search_condition ] [ GROUP BY group_by_expression ...

  2. mysql中的select语句where条件group by ,having , order by,limit的顺序及用法

    -- 语法: SELECT select_list FROM table_name [ WHERE search_condition ] [ GROUP BY group_by_expression ...

  3. MySQL select from join on where group by having order by limit 执行顺序

    书写顺序:select [查询列表] from [表] [连接类型] join [表2] on [连接条件] where [筛选条件] group by [分组列表] having [分组后的筛选条件 ...

  4. Mysql查询优化汇总 order by优化例子,group by优化例子,limit优化例子,优化建议

    Mysql查询优化汇总 order by优化例子,group by优化例子,limit优化例子,优化建议 索引 索引是一种存储引擎快速查询记录的一种数据结构. 注意 MYSQL一次查询只能使用一个索引 ...

  5. MySql学习(二) —— where / having / group by / order by / limit 简单查询

    注:该MySql系列博客仅为个人学习笔记. 这篇博客主要记录sql的五种子句查询语法! 一个重要的概念:将字段当做变量看,无论是条件,还是函数,或者查出来的字段. select五种子句 where 条 ...

  6. hive的高级查询(group by、 order by、 join 、 distribute by、sort by、 clusrer by、 union all等)

    查询操作 group by. order by. join . distribute by. sort by. clusrer by. union all 底层的实现 mapreduce 常见的聚合操 ...

  7. where / having / group by / order by / limit 简单查询

    目录 1.基础查询 -- where 2. group by 与 统计函数 3. having 4.where + group by + having + 函数 综合查询 5. order by + ...

  8. 操作数据表中的记录——SELECT (where表达式、GROUP BY、HAVING、LIMIT)

    原文链接:http://www.ifyao.com/2015/01/26/%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E8%A1%A8%E4%B8%AD%E7%9A%84 ...

  9. hive的strict模式;where,group by,having,order by同时使用的执行顺序

    主要限制三种情况 (1) 有partition的表查询需要加上where子句,筛选部分数据实现分区裁剪,即不允许全表全分区扫描,防止数据过大 (2) order by 执行时只产生一个reduce,必 ...

  10. hive:(group by, having;order by)的使用;group by+多个字段,以及wiki说的group by两种使用限制验证

    hive> select * from app_data_stats_historical where os='1' group by dt limit 100; 出现结果如下: 2014-01 ...

随机推荐

  1. linux上部署SpringBoot项目及遇到的问题

    打开sftp步骤, 在显示的已连接的窗口上右键, 选择connect SFTP Session就可以打开文件上传的窗口 从windows上传文件到linux, 首先linux需要先切换到想要保存文件的 ...

  2. hashcode相等两个类一定相等吗?equals呢?相反呢?

    hashCode相等,equals也不一定相等, 两个类也不一定相等 equals相同, 说明是同一个对象, 那么hashCode一定相同 哈希表是结合了直接寻址和链式寻址两种方式,所需要的就是将需要 ...

  3. Teradata的profile使用

    1.proflie优势 使用profile可以批量管理用户参数,尤其是在一批用户具有相同的参数配置时,十分便捷. 2.profile可配置用户参数 [Account id][Default datab ...

  4. C#基础知识之托管代码和非托管代码

    什么是托管代码(managed code)? 托管代码(Managed Code)就是中间语言(IL)代码,在公共语言运行库(CLR)中运行.编译器把代码编译成中间语言,当方法被调用时,CLR把具体的 ...

  5. 数位dp D - Count The Bits

    题目:D - Count The Bits 博客 #include <cstdio> #include <cstring> #include <cstdlib> # ...

  6. 使用IntelliJ IDEA和Maven管理搭建Web开发环境(以Spring MVC为例)(二)

    前言:在使用IntelliJ IDEA和Maven管理搭建Web开发环境(以Spring MVC为例)(一)中已经介绍了如何对web基础环境进行搭建,这里主要演示,如何对spring环境进行搭建,然后 ...

  7. 「AHOI / HNOI2017」单旋

    「AHOI / HNOI2017」单旋 题目链接 H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能多,效率高,掌握这种 ...

  8. 【转】iOS中修改AVPlayer的请求头信息

    在开发中, 我们经常需要在网络请求时修改HTTP/HTTPS的请求头信息 1.普通AFN请求 #import "LMHTTPSessionManager.h" #import &l ...

  9. Linux:Day6(上) egrep、条件测试

    egrep及扩展的正则表达式: egrep = grep -E 扩展正则表达式的元字符: 或者:a | b 练习: 1.显示当前系统root.centos或user1用户的默认shell和UID: 2 ...

  10. pytorch例子学习——TRANSFER LEARNING TUTORIAL

    参考:https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html 以下是两种主要的迁移学习场景 微调convnet : ...