• 五种函数、子查询及分页查询思路


    聚合函数:
    【加上列名是为了多个放在一起时易于区分!】

    平均分:avg()
    书写格式:select avg(字段名)as '函数字段名' from 表名称

    最大值:max()
    书写格式:select max(字段名)as '函数字段名' from 表名称

    最小值:min()
    书写格式:select min(字段名)as '函数字段名' from 表名称

    求和:sum()
    书写格式:select sum(字段名)as '函数字段名' from 表名称

    数据条数:COUNT(*)
    书写格式:select 字段名,COUNT(*) from 表名称

    使用:求每种系列有多少个,它们的平均价格是多少

    PS:
    -------------------------------------------------------------
    数学函数:

    取上限:ceiling()
    书写格式:select ceiling(字段名)from 表名称

    取下限:floor()
    书写格式:select floor(字段名)from 表名称

    绝对值:abs()
    书写格式:select abs(值)

    派,圆周率: PI()
    书写格式:

    四舍五入:ROUND()
    书写格式:select round(要四舍五入的值,要保存的位数)

    开根号:SQRT()
    书写格式:select sqrt(要开根号的值)

    平方根:SQUARE()
    书写格式:select square(要开平方的值)

    -------------------------------------------------------------
    转换函数:

    cast(列 as 类型)
    书写格式:select '字符串类型的值'+ cast(字段名 as 要转换成的数据类型) from 表名称

    convert(类型,列)
    书写格式:select '字符串类型的值'+ convert (要转换成的数据类型,字段名) from 表名称

    -------------------------------------------------------------
    字符串函数:

    转换大写:upper()
    书写格式:select upper(值或者字段名)

    转换小写:lower()
    书写格式:select lower(值或者字段名)

    去空格:trim()
    书写格式:select ltrim (要去空格的内容) --去左空格
    select rtrim (要去空格的内容) --去右空格

    左截取:left(值,长度)
    书写格式:select left(要截取的内容,要截取的长度)
    select right(要截取的内容,要截取的长度)

    长度:len()
    书写格式:

    替换:replace(值,内容,替换内容)
    书写格式:select repiace (即将要替换的所有原内容,要替换掉的原内容,要替换进去的新内容)

    翻转:reverse()
    书写格式:select reverse (要翻转的内容)

    字符串转换:str(值,保留位数,小数位数)
    书写格式:select str (数值,保留的位数,要保留的数中小数的位数)
    【小数点也占一位!】

    字符串截取:substring(值,索引,位数)
    书写格式:select substring (要截取的内容,开始的索引,截取的长度)
    【注意:字符串截取时的索引从1开始!】
    PS:
    -------------------------------------------------------------
    时间日期函数:

    获取当前时间:GetDate()
    书写格式:select getdate()

    获取年月日:year() month() day()
    书写格式:select year(字段名),month(字段名)from 表名称

    判断日期是否正确:isdate()
    书写格式:select isdate (datetime类型的值)

    添加时间:dateadd(添加类型,数量,值)
    书写格式:select dateadd(添加类型,数量,要加的基数数据)

    返回周几:datename(weekday,值)
    书写格式:select datename (weekday,datetime数据类型的值)

    返回这一天是当月第几天:datename(day,值)
    书写格式:select datename (day,datetime数据类型的值)

    返回这一天是当年第几天:datename(dayofyear,值)
    书写格式:select datename (dayofyear,datetime数据类型的值)
    PS:
    -------------------------------------------------------------
    子查询:
    any
    select *from 字段名 where 该字段名 >/</>=/<= any
    (select 字段名 from 表名称 where 该字段名 in/not in (值,值,、、、,值))

    all
    select *from 字段名 where 该字段名 >/</>=/<= all
    (select 字段名 from 表名称 where 该字段名 in/not in (值,值,、、、,值))


    in
    select *from 字段名 where 该字段名 in (值,值,、、、、,值)

    not in
    select *from 字段名 where 该字段名 not in (值,值,、、、、,值)

    【注意!子查询中,语句作为参数时,查询出来的数据可以是多行,但必须是同一列!】

    查询价格比宝马的任意一款车的价格高的车辆信息
    select *from car where price > any
    (select price from car where name like '%宝马%')
    或者写作:
    select *from car where price >(select min(price)from car where name like '%宝马%')

    查询价格比宝马的所有车的价格高的车辆信息
    select *from car where price >(select max(price)from car where name like '%宝马%')

    boss:比宝马的最低价格高的不是宝马的那些车
    select *from car where price>
    (select min(price)from car where name like '%宝马%' )
    and code not in
    (select code from car where name like '%宝马%')

    分页查询思路
    select top 常量值 *from 表名称 where 字段名 not in(select top 变量公式 字段名 from 表名称)

  • 相关阅读:
    算法笔记--贪心
    算法笔记--递归
    算法笔记--哈希
    算法笔记--散列
    算法笔记--排序算法
    算法笔记--简单编程训练
    算法笔记--简单模拟
    算法笔记--注意事项
    3.4 空间滤波
    【解题报告】【概率DP入门】 P1850 换教室
  • 原文地址:https://www.cnblogs.com/123lucy/p/5571342.html
Copyright © 2020-2023  润新知