顺序很重要

  每次看数据库的一些语法时,都很自然的略过那一大堆的规则,比如说线下面这段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;

  

 

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. Debian9安装vim和vim无法右键鼠标粘贴解决方法

    问题描述: Debian9有时候安装的时候没有vim,在centos用习惯了vim 1.Debian安装vim: root@kvm1:/etc/network# apt-get install vim ...

  2. 【Teradata】配置PE和AMP(congfig和reconfig工具、vprocmanager)

    The Reconfiguration and Configuration utilities are used to define the AMPs and PEs that operate tog ...

  3. 【Linux基础】文件处理实例

    1.文件拆分 //每4000行拆分一个文件 epms_t_ep_fx_stl_xy_20190129.dat 2.行处理 //查找第二列为711611且第三列为711100记录,打印行号和整行数据 a ...

  4. UVA215-Spreadsheet Calculator(模拟+拓扑排序)

    Problem UVA215-Spreadsheet Calculator Accept:401  Submit:2013 Time Limit: 3000 mSec Problem Descript ...

  5. [tool] google搜索的正确使用姿势(待补全)

    第一,也是非常重要的前提,请一定要能FQ.如果这条没有解决,没有往下的必要 现在我假设你已经能FQ了,个人推荐使用搜索引擎的顺序: Google>微软bing国际搜索>百度 (百度总能给你 ...

  6. 转://oracle Wallet在expdp/impdp中使用场景

    oracle Wallet的使用(即内部加密技术TDE(Transparent Data Encryption )) 1. TDE是Oracle10gR2中推出的一个新功能,使用时要保证Oracle版 ...

  7. ROS 小乌龟测试

    教程 1.维基 http://wiki.ros.org/cn/ROS/Tutorials 2. 创客智造 http://www.ncnynl.com/category/ros-junior-tutor ...

  8. ORA-4031 错误故障排除与诊断[视频] (Doc ID 2016002.1)

    Copyright (c) 2019, Oracle. All rights reserved. Oracle Confidential.     ORA-4031 错误故障排除与诊断[视频] (Do ...

  9. nova系列一:虚拟化介绍

    一 什么是虚拟化 虚拟化说白了就是本来是一个完整的资源,切分或者说虚拟成多份,让这多份资源都使用起来,物尽其用,减少了浪费,提高了利用率,省了钱. 虚拟化(Virtualization)技术最早出现在 ...

  10. RPC通信原理(未完,先睡觉)

    一 背景 OpenStack 各组件之间是通过 REST 接口进行相互通信,比如Nova.Cinder.Neutron.Glance直间的通信都是通过keystone获取目标的endpoint,即ap ...