星空网 > 软件开发 > 数据库

数学函数,字符串函数,聚合函数

1、

--数学函数;ABS(-8)绝对值、ceiling(3.12) 取上线、floor取下限、power(2,3)几次方、
--round()四舍五入
--sqrt开平方根、squar平方


--字符串函数
--ASCII 返回字符串最左边的字符ascii码
select ASCII('name')
select ASCII(name)from biao--查看所有人名的首字符的ascii码
--char 将ascii码转换成字符
select CHAR(70)
select CHAR(yuwen)from biao--讲所有语文分数转换成字符
--注意,(整数)所转换的表达式或者常量需要在0-256之间,超出的话输出n

--LEN 返回字符串的长度
select LEN('asdfghh')
select LEN(name)from biao --显示所有姓名的长度

--charindex 返回字符串首个字符出现在某个字符串从头开始为几的索引
select CHARINDEX('d','asdfghhjkkhg')--索引从1开始
select CHARINDEX('1990',birth) from studant --查看在生日里面出现的索引


--difference 返回相似度 用0——4表示相似度
select DIFFERENCE('asddfghjk','adfjkgh')
--LEFT 表示从左边截取字符串
select LEFT('asfgdsssdgh',4)

--RIGHT 从右边
select right('asfgdsssdgh',4)

--lower 全部转化成小写
select LOWER('asfasdgsgDDFQWEG')
--upper 大写
select upper('asfasdgsgDDFQWEG')

--Ltrim 去掉左边的空格
select LTRIM(' asd ')
--Rtrim 去掉右边的空格
select RTRIM(' asd ')

--patindex 相当于charindex 返回字符串所在字符中的首字符索引位
select PATINDEX('%df%','ssdfghss')

--Replace 查找替换
select REPLACE(sex,'女','姑娘')from biao--只显示,不更改
--replicat 复制粘贴
select REPLICATE('asd ',3)
--reverse 翻转
select REVERSE('asdfgghjk')

--space 空格
select 'a'+SPACE(5)+'bc'

--str 强制转换成字符串
select STR(123456.222,5,1)--参数1是需要转换的数值,参数2是转换之后保留的长度
--参数3是小数点后需要保留的位数
--注意,参数2在小于参数1整数部分位数时无法转换

--stuff
--从第几个索引的位置,看看需不需要向后删除几位,然后将需要插入的内容插入
--参数1是需要**入的字符串
--参数2是从第几个索引开始
--参数3是是否需要向后删除几个字符
--参数4是新插入的字符
select STUFF('asddfg',3,2,'HELLO')

--substring
--截取字符串
--参数1是被截取的字符串
--参数2是从哪个索引开始
--参数3是截取的长度
select SUBSTRING('asdfghjjgfddrtyy',4,5)

2、

--查询全部
select * from student
--查询李四的所有信息
select * from student where name ='李四'
--查询李四的成绩
select score from student where name = '李四'
--添加一条数据(若之前有删除数据的情况,就会放在从头开始删除数据的第一行)
insert into student values(7,'铃铛','1993-4-5','男',79)

--给张全蛋改名
update student set name='李狗蛋'where name ='张全蛋'
--给铃铛改性别
update student set sex ='女' where name ='铃铛'

--赵六转学
delete from student where name ='赵六'

--排序,升序
select * from student order by score
--降序
select * from student order by score desc
--只要最高分
select top 1 * from student order by score desc
--只要最低分
select top 1 * from student order by score

--查看所有学生姓名(利用as另起别名显示)
select name as 学生姓名 from student
--查看所有学生的姓名和分数(另起别名)
select name as 学生姓名,score as 分数 from student

--查看所有姓王的学生的所有信息(模糊查询)
select * from student where name like '王%'
--查看所有1993年的学生的所有信息
select * from student where birth like '1993%'
--查看所有生日最后一天带6的学生的信息
select * from student where birth like '%6'
--查看姓名中间字为狗的学生的所有信息
select * from student where name like '%狗%'
--查看姓李的并且名字只有两个字的学生的所有信息
--下划线模糊查询,只代表一个字符(不常用)
select * from student where name like '李_'


--查看分数在80以上的学生的所有信息
select * from student where score >=80
--查看60-80之间分数的所有学生信息
select * from student where score between 60 and 80
--查看分数低于60的学生的姓名
select name as 学生姓名 from student where score <60

--修改分数在70-80之间的所有学生的sex=男
update student set sex ='男' where score between 70 and 80

--计算345678 + 789456
select 345678+789456

--查询所有人名 distinct 去重
select distinct name from student
--查看有几个性别
select distinct sex from student

--in in后面的括号中是用来判断是否有此(条件)的,可以看做是元素
--查看姓名为李狗蛋和铃铛的所有信息
select * from student where name in('李狗蛋','铃铛')

--引号里面括号外加下划线,意思是选择里面任意一个值,不常用
select * from student where name like'_[李狗蛋,铃铛]'

 

--聚合函数:sum avg max min count
--求所有分数的总和
select SUM(score) as 总和 from student

--求所有分数的平均分
select AVG(score) as 平均分 from student

--查看最高分
select MAX(score) as 最高分 from student
--查看最低分
select MIN(score) as 最低分 from student

--查看总人数
select COUNT(*) as 总人数 from student
--查看叫李四的有几个
select COUNT(*) as 数量 from student where name ='李四'

--group by 分组
--按照男女来分组
select sex from student group by sex
--按照男女来分组,分组之后求平均分
select sex as 性别 , AVG(score) as 平均分 from student group by sex
--分别查看男女的数量
select sex as 性别, COUNT(*) as 人数 from student group by sex
--分别查看男女,并且分数在70以上的人的个数
select sex as 性别, COUNT(*) as 人数 from student where score >=70 group by sex
--分别查看男女,并且分数在70以上的并且人数超过3个的组
select sex as 性别, COUNT(*) as 人数 from student where score >=70 group by sex having COUNT(*)>3


--按照男女来分组,分组之后求平均分,并且平均分>70
select sex as 性别 , AVG(score) as 平均分 from student group by sex having AVG(score)>70

 

3、实例

create table cangku
(
code int,
name varchar(50),
zong int,
price decimal(18,2)
)
go
insert into cangku values(1,'苹果',30,2.9)
insert into cangku values(2,'梨',30,2)
insert into cangku values(3,'西瓜',37,1.9)
insert into cangku values(4,'馒头',30,1)
insert into cangku values(5,'猪肉',20,5)
insert into cangku values(6,'茄子',45,3.6)
insert into cangku values(7,'黄瓜',60,2.4)
insert into cangku values(8,'白菜',30,0.8)
insert into cangku values(9,'哈密瓜',70,3)
insert into cangku values(10,'南瓜',30,1.89)
--卖掉了第3种产品3个
update cangku set zong=34 where code=3
--卖掉第4种产品7个
update cangku set zong=23 where code=4

--卖掉第5种产品5个
update cangku set zong=15 where code=5

--查看现在货物库存最少的商品全部信息
select top 1* from cangku order by zong
--货物最少的商品进货补齐30件
update cangku set zong=30 from cangku where code=5
--现在第3种,第4种,第5种产品都涨价一块
update cangku set price=price+1 from cangku where code in(3,4,5)
--第6种第7种第8种统统减价一块
update cangku set price=price-1 from cangku where code in(6,7,8)

--卖掉了第6种产品3个
update cangku set zong= zong-3 from cangku where code=6
--卖掉第7种产品3个
update cangku set zong= zong-3 from cangku where code=7
--卖掉第8种产品9个
update cangku set zong= zong-9 from cangku where code=8
--查看现在货物库存最少的商品全部信息
select top 1* from cangku order by zong
--货物最少的商品进货补齐30件
update cangku set zong=30 from cangku where code=5
select*from cangku

 




原标题:数学函数,字符串函数,聚合函数

关键词:函数

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

解密离岸账户:开通银行离岸账户的全面指南 :https://www.kjdsnews.com/a/1838596.html
解密离岸账户:开通银行离岸账户的全面指南 :https://www.xlkjsw.com/news/84103.html
NRA账户中的欧元可以转成美元吗? :https://www.kjdsnews.com/a/1838597.html
NRA账户中的欧元可以转成美元吗? :https://www.xlkjsw.com/news/84104.html
在美国开公司需要是本地人吗 :https://www.kjdsnews.com/a/1838598.html
在美国开公司需要是本地人吗 :https://www.xlkjsw.com/news/84105.html
海外KOL推广:与潮流文化共舞,打造年轻态时尚品牌形象:https://www.xlkjsw.com/news/92295.html
DTC个护品牌Svish Ropes完成新一轮融资:https://www.kjdsnews.com/a/1842223.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流