• 常用的 SQL 函数


       SQL 函数

          聚合函数(针对数字列):

      AVG:求平均分
      COINT: 计算个数
      MAX: 求最大值
      MIN: 求最小值
      SUM: 求和

           数学函数():
      ABS:     绝对值
      CEILING: 上限取整
      floor:  取整 
      pi:     圆周率
      round:  第一个参数是要进行四舍五入的数,第二个参数是要四舍五入到小数点之后的几位
      rand(): 随机生成
      sqrt:   求平方根,开根号
      square: 求平方
     
           日期时间函数:
      DATEADD:    添加
      datediff:   取时间差 
      datename:   单独返回日期时间中的年月日时分秒,或者星期,第几周
      datepart:   取指定字段的整数
      YEAR:       返回年份
      month:      返回年份的第多少个月份
      day:        返回月份的第多少天
      isdate:     判断日期时间
      getdate:    返回数据库服务时间
      SYSDATETIME:获取电脑的系统时间

    函数后面加 as 可以给函数列起别名比如: as 别名

          聚合函数的应用:

    ---------三班男生比二班多几个人
    select  (select COUNT(*)  from zuoye where xingbing='男' and banji='三班')

    ---------结合分组使用聚合函数,分别对每组进行计算显示,having筛选
    select COUNT(*),avg(shengao),banji from zuoye group by banji having COUNT(*)>3

    ---------查询每个班里比这个班的平均身高高的同学的所有信息
    select*from zuoye as a where shengao>(select AVG(shengao) from zuoye as b where b.banji=a.banji)

    ---------查询身高最高的人
    select MAX(shengao) from zuoye

    ---------查询身高最矮的人
    select MIN(shengao) from zuoye

    ---------查询所有人身高的和
    select SUM(shengao) from zuoye

          数学函数:

    ---------绝对值
    select ABS(-1)  

    ---------ceiling上限取整
    select CEILING(1.9)
    select CEILING(1.1)

    ---------floor取整
    select floor(1.1)
    select floor(1.9)

    ---------pi圆周率
    select pi()

    ---------round第一个参数是要进行四舍五入的数,第二个参数是要四舍五入到小数点之后的几位
    select round(3.597,2)

    ---------rand()随机生成
    select RAND()

    ---------sqrt求平方根,开根号
    select sqrt(16)

    ---------square求平方
    select square(4)

    ---------求身高平方根之后取 上限整数 然后等于14的同学的信息显示出来
    select *from zuoye where ceiling (SQRT(shengao))=14
    select xingming,shengao,FLOOR(SQRT(shengao))from zuoye where ceiling (SQRT(shengao))=14

          日期函数:

    ---------DATEADD 添加
    select DATEADD(YEAR,1,'20000229')--加年
    select DATEADD(month,1,'20000331')--加月
    select DATEADD(day,1,'20000229')--加天

    ---------datediff 取时间差
    select DATEDIFF (YEAR,'20111211','20141116')--相差几年
    select DATEDIFF (month,'20111211','20141116')--相差月
    select DATEDIFF (day,'20111211','20141116')--相差天
    select DATEDIFF (week,'20111211','20141116')--相差星期

    ---------datename 单独返回日期时间中的年月日时分秒,或者星期,第几周
    select datename (YEAR,'20261211')
    select datename (week,'20261211')
    select datename (weekday,'20261211')

    ---------datepart 取指定字段的整数
    select datepart (YEAR,'20261211')
    select datepart (WEEK,'20261211')
    select datepart (WEEKDAY,'20261211')

    ---------YEAR返回年份
    select year('20141125')

    ---------month返回年份的第多少个月份
    select month('20141125')

    ---------day返回月份的第多少天
    select DAY('20141125')

    ---------getdate返回数据库服务时间
    select getdate()

    ---------getdate返回数据库服务时间
    select getdate()

    ---------isdate判断日期时间
    select isdate('20140216')

    ---------SYSDATETIME获取电脑的系统时间
    select SYSDATETIME()

    ---------显示在1988年出生的人
    select *from zuoye where YEAR(shengri)=1998

    ---------显示所有人的名字跟生日

    select xingming,YEAR(shengri)from zuoye

  • 相关阅读:
    伺服电机常见故障分析汇总
    PLC常见四大故障及其处理方法
    Qt QString的arg()方法的使用
    Qt 线程池QThreadPool类、QRunnable类
    C++线程池的实现
    C++ List的用法
    C++语言堆栈的详细讲解
    QT中的QQueue类、C++中的queue类
    Qt platform plugin 'windows' 问题的解决方法
    电气接地的相关介绍
  • 原文地址:https://www.cnblogs.com/2041388565m/p/4122869.html
Copyright © 2020-2023  润新知