学数据库这么久了?SQL数据库基础函数都搞懂了吗?安排!

126 阅读1分钟

SQL的基础函数

lower

select lower(name) from students #将students表的所有name字段的数据全都转为小写打印出来

upper

select upper(name) from students #将students表的所有name字段的数据全都转为大写打印出来

length

select length(name) from students #将students表的所有name字段的数据的长度打印出来

substr

select substr(name,1,3) from students; #name字段从1的位置向后截取3位打印出来	例如David -->Dav
select substr(name,2,3) from students; #name字段从2的位置向后截取3位打印出来	例如David -->avi

注意:sql的下标是从1开始

concat

select concat(name,'123') from students; #将students表的所有name字段的数据后面拼接123打印出来	例如David -->David123

replace

select replace(name,'a','666') from students; #将students表的所有name字段的数据中的a替换为666 例如David -->D666vid

ifnull

select ifnull(hobby,'学习') from students #判断,如果hobby字段的值是null的用学习替换

round & ceil & floor

select round(score) from students #四舍五入取整

select round(score,1) from students #四舍五入,保留一位小数

select ceil(score) from students #向上取整 7.1 --> 8

select floor(score) from students #向下取整 7.9 --> 7

now

select now() #现在的年与日,时分秒

select curdate() #现在的年月日

select curtime() #现在的时分秒

year & month & day

select year(now()) #取现在的年

select month(now()) #取现在的月

select day(now()) #取现在的日

hour()时&minute()分&second()秒

select hour(now()) #取现在的时

select minute(now()) #取现在的分

select second(now()) #取现在的秒

标签: [数据库] 在这里插入图片描述