码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-06-08 21:42:41      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:

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

平均分: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 ‘字符串类型的值‘+ convert (字段名 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 表名称)

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

标签:

原文地址:http://www.cnblogs.com/123lucy/p/5571342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!