zoukankan      html  css  js  c++  java
  • 常用SQL语句的整理--SQL server 2008(查询二--)

    SQL中的内置函数:

    --聚合函数COUNT--查询表中数据的条数

    select COUNT(*) from person 
    select COUNT(*) from person where age in(18,19,20)--可以跟想要的上一节讲的所有函数

    --MIN,MAX,Avg,SUM

    select MIN(age) from person 
    select max(age) from person 
    select avg(age) from person 
    select sum(age) from person 

    --数据分组---根据年龄分组,然后取出分组后的数据

    select age,COUNT(*) from person group by age

    注:如果所用字段没有出现在group by后面,是不能用带select语句中使用的(但是聚合函数是可以的),我们来看两个例子

    select age,COUNT(*)as 数量,AVG(age) from person group by age
    错误用法:select age,COUNT(*)as 数量,username from person group by age
    ---错误提示:选择列表中的列 'person.username' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。

    Having是对分组后信息的过滤,能用的列和select中能用的列是一样,关于having和where的区别,这里暂时不深入去说了,但是要明白一点是,having不能代替where

    select age,COUNT(*)as 数量 from person group by age having age>15
    ---根据年龄分组统计数量,并且选择年龄大于15的组别

    --去除重复数据

    select distinct age from person

    --联合结果集(把查询结果结合成为1个查询结果)注:上下两个查询语句字段必须一致(名字,类型,个数都必须一致)

    select username,age from person
    union
    select username,age from student

    注:union和union all 的区别。前者合并重复数据,后者不合并,由于union需要对数据进行扫描和对比,所以效率低,所以如果不是要合并数据的话,建议用union all

    --查询每一个人的姓名和年龄,并且计算年龄综合,然后放置最后一列中

    select username,age from person union all
    select '合计',SUM(age)from person
  • 相关阅读:
    Java 性能优化实战记录(3)--JVM OOM的分析和原因追查
    Map/Reduce的类体系架构
    Map/Reduce个人实战--生成数据测试集
    Ubuntu 16.04下的安装RabbitMQ
    Zookeeper数据存储总结
    Zookeeper原理分析之存储结构ZkDatabase
    Zookeeper原理分析之存储结构TxnLog
    Zookeeper原理分析之存储结构Snapshot
    Kafka迁移与扩容工具用法
    Sublime Text 格式化JSON-pretty json
  • 原文地址:https://www.cnblogs.com/guyali/p/5379574.html
Copyright © 2011-2022 走看看