zoukankan      html  css  js  c++  java
  • 五种函数、子查询和分页查询

    聚合函数:主要是操作列

    平均分:avg()
    格式:select avg(列名) as '自定义列名'from 表名 --单独计算一列的平均分

    最大值:max()
    格式:select max(列名) as '自定义列名'from 表名

    最小值:min()
    格式:select min(列名) as '自定义列名'from 表名

    求和:sum()
    格式:select sum(列名) as '自定义列名'from 表名

    数据条数:COUNT(*)
    格式:select count(*) from 表名

    使用:求每种系列有多少个,它们的平均价格是多少
    格式:select 列名,count(*) as '自定义列名',avg '自定义列名'from 表名 group by 列名 --列名是一个,每种系列有多少个

    -------------------------------------------------------------
    数学函数

    取上限:ceiling()
    格式:select ceiling(列名) from 表名 --小数点后大于零,就进一位

    取下限:floor()
    格式:select floor(列名) from

    绝对值:abs()
    格式:select abs(-5) --select把结果映射到结果框,abs把负值变成正值
    print -5 --把结果映射到消息框

    派,圆周率: PI()

    四舍五入:ROUND()
    ps:select roung (pi(),2)--2保留小数点后几位。pi括号不需要放东西,放哪儿就是π

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

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

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

    cast(列 as 类型)ps:select '油耗'+cast(oil as nvarchar(50)) from car

    convert(类型,列)ps:select 列名+convert(nvarchar(50),oil) from car

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

    转换大写:upper()
    select upper() from

    转换小写:lower()

    去空格:trim()--中间的空格去不掉
    select ltrim()--去掉左边的空格
    select rtrim()--去掉右边空格

    左截取:left(值,长度)
    select left ('abcde',3)--截取结果abc
    右截取:right(值,长度)
    select right ('abcde',3)--截取结果cde
    字符串截取(值,索引,长度)
    select substring ('abcde',2,2)--截取结果bc,索引从1开始

    长度:len()
    select len('abcde')--结果5

    替换:replace(值,内容,替换内容)
    select replace ('aaaabbaaaa','bb','hehe')--结果aaaaheheaaaa

    翻转:reverse()
    select reverse ('abcde')--edcba

    字符串转换:str(值,保留位数,小数位数)
    select str (3.14,2,1)--结果3。2一共保留两位,小数点算一位
    -------------------------------------------------------------
    时间日期函数:

    获取当前时间:GetDate()
    select getdate() --获取当前时间

    获取年月日:year() month() day()
    select year(列名),month(列名),day(列名)from 表名

    判断日期是否正确:isdate()
    select isdate(datetime类型)--bool类型,结果1或0

    添加时间:dateadd(添加类型,数量,值)
    ps:select dateadd(day,2,getdate())

    返回周几:datename(weekday,值)
    select datename (weekday,getdate())
    组合使用:select datename (weekday,dateadd(day,2,getdate()))

    返回第几天:datename(day,值):select datename (day,getdate())--返回在这个月中的第几天
    datename(dayofyear,值):select datename (dayofyear,getdate())返回在一年中的第几天

    -------------------------------------------------------------
    子查询:
    把sql语句当成一个值来使用:
    select 要查询的子级列名 from 子级表名 where 要查询的子级主键列名 = (select 要查询的父级字段名 from 查询的父级字段名所在的表 where 父级主键列=对应值)

    in:
    select *from 表名 where 列名 in(值,值)--当那一列的值是值其中就行
    not in:
    select *from 表名 where 列名 not in(值,值)--当那一列的值不在值里面的
    any:
    select *from 表名 where 列名 >any(select 列名from 表名 where 列名 in(值,值))--any 任何一个,大于任何一个any括号里的值就符合条件
    all:
    select *from 表名 where 列名 >all(select 列名from 表名 where 列名 in(值,值))--all 所有值都。比所有值都大才符合条件

    ps:select *from car where powers >any(select powers(马力)from 表名 where oil(油耗) 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 > all (select 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 5(前5条) *from car where ids not in( select top 页数乘以5 ids from car )

  • 相关阅读:
    Centos7部署jenkins
    查看杀死django进程
    【填坑】python3 manage.py migrate:?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
    Amazon S3 加密
    向 Elastic Beanstalk 环境中添加数据库
    Amazon RDS 监控概览
    AWS Lambda 环境变量
    使用 DynamoDB Auto Scaling 自动管理吞吐容量
    使用 Amazon Aurora Auto Scaling 扩展 Aurora 副本
    ECS 服务 Auto Scaling 和 Application Auto Scaling
  • 原文地址:https://www.cnblogs.com/sunshuping/p/5574242.html
Copyright © 2011-2022 走看看