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
  • 相关阅读:
    对Android开发者有益的40条优化建议
    git推送tag到远端服务器
    详细注释!二维码条码扫描源码,使用Zxing core2.3
    探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法
    android的logcat详细用法!
    【Android】开源项目汇总-备用
    android 中theme.xml与style.xml的区别
    Android实现对HOME键的捕获和屏蔽
    java中newInstance()和new()
    java Stack
  • 原文地址:https://www.cnblogs.com/guyali/p/5379574.html
Copyright © 2011-2022 走看看