Home > Database > Mysql Tutorial > body text

黑马程序员――SQL常用函数_MySQL

WBOY
Release: 2016-06-01 13:32:36
Original
1666 people have browsed it

bitsCN.com

黑马程序员——SQL常用函数

 

列举了一些SQL中的常用函数。

 

数字函数

 

ABS()          求绝对值(让我想起了ABS防抱死系统)

 

CEILING()    舍入到最大整数,-3.6舍入到-3

 

FLOOR()      舍入到最小整数,-3.6舍入到-4

 

ROUND()     四舍五入,ROUND(3.141, 2) 需要传入两个参数,前一个为操作数,后一个为精度

 

字符串函数

 

LEN()           计算字符串长度

 

LOWER()     转换为小写字符

 

UPPER()       转换为大写字符

 

LTRIM()        去左空格

 

RTRIM()        去右空格

 

SUBSTRING(string, start_position, lenth)    字符串截取函数,从start_position处开始截取长度为lenth

 

日期函数

 

GETDATE()       取当前日期

 

DATEADD(datepart, number, date)     函数用于计算增量后的日期,datepart 是计量单位,date 是需要操作的日期

 

                                                           datepart 可选取:year, quarter, month, dayofyear, day, week, weekday, hour, minute, second

 

DATEDIFF(datepart, startdate, enddate)   根据datepart求得两个日期之间的差值

 

DATEPART(datepart, date)                       返回日期的指定部分

 

类型转换

 

CAST(expression as type)

 

CONVERT(type, expression)

 

空值处理函数

 

ISNULL(expression, value)          判断若 expression 不为空返回 expression,否则返回 value

 

select ISNULL(name, '佚名') from Person

 

单值判断

 

类似于 switch case 语句。

 

CASE expression

 

WHEN value1 THEN return1

 

WHEN value2 THEN return2

 

ELSE return3

 

END

 

当 when 后做范围判断时,case后可以没有表达式。

 

ROW_NUMBER() 函数

 

作用是统计行号。

 

ROW_NUMBER()是开窗函数,不能出现在 where 中,只能出现在 select、order by 中。

 

select * from

 

(select ROW_NUMBER() OVER(order by salary) as rownum, id, name, from Person) as e1

 

where e1.rownum > 3 and e1.rownum

 

这样可以随意取到想要的数据行。

 

-The End-

 

 

 

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!