SQL 必知必会--函数篇

对SQL的基础函数做复习回顾,本篇涉及的函数知识如下:


好了,下面开始复习:

SQL Aggregate 函数计算从列中取得的值,返回一个单一的

Max() 函数

作用:返回指定列的最大

语法:

Select max (column name) from table name where condition;

例子:从exam表里获取 math列的 最大值 

Select max (math)  as max_math from exam;

Min() 函数

作用:返回指定列的最小

语法:

Select min (column name) from table name where condition;

例子:exam表里获取art列的 最小值

Select min (art)  as min_math from exam;

Count( ) 函数

作用和语法:

返回符合指定条件的行数

Select count (column name) from table name where condition; (NULL 不计入)

返回表中的记录数

Select count (*) from table name;

返回指定列的不同值的数目

Select count (distinct column name) from table name where condition;

PSCOUNT(DISTINCT) 适用于 ORACLE Microsoft SQL Server,但是无法用于 Microsoft Access

例子:exam表里获取art列大于60分的数量

Select count (art) from exam where art >= 60;

Avg( ) 函数

作用:返回数值列的平均

语法:

Select count ( column name) from table name;

例子:从exam表里获取art列的平均值

Select avg (art) as avg_art_score from exam;

First( ) 函数

作用:返回指定的列中第一个记录的

语法:

Select first ( column name) from table name;

PS只有 MS Access 支持 FIRST()

例子:从exam表里获取art列的第一个值

Select first (art) as first_ score from exam;

实现相同操作:

Mysql 使用 limit offset

Select art from exam order by id ASC limit 1;

Sql server 使用 top n

Select top 1 art from exam order by id ASC;

Oracle使用 rownum

Select art from exam order by id ASC where rownum <= 1;

Last( ) 函数

作用:返回指定的列中最后一个记录的

语法:

Select last ( column name) from table name;

例子:从exam表里获取art列的最后一个值

PSfirst() 函数相同只有 MS Access 支持 last() ,其他数据库实现方法参见实现first()函数的形式

Sum( ) 函数

作用:返回数值列的总

语法:

Select sum ( column name) from table name;

例子:从 class 表里获取 num 列的总和

Select sum (num) as total_num from class;

SQL Scalar 函数基于输入值,返回一个单一的值

Ucase( ) 函数

作用:把字段的值转换为大写

语法:

Select ucase ( column name) from table name;

例子:将class 表里面的name 列都转化为大写

Select ucase (name) as newName from class;

PS:sql server使用upper()

Lcase( ) 函数

作用:把字段的值转换为大写

语法:

Select lcase ( column name) from table name;

例子:将class 表里面的name 列都转化为小写

Select lcase (name) as newName from class;

PS:sql server使用lower()

Mid( ) 函数

作用:用于从文本字段中提取字

语法:

Select mid ( column name, start[,length]) from table name;

参数

描述

column name

必需。要提取字符的字

start

必需。规定开始位置(起始值是 1

length

可选。要返回的字符数。如果省略,则 mid( ) 函数返回剩余文

例子:将class 表里面的name 列取前四个字符

Select mid (name,1,4) as short_name from class;

Len( ) 函数

作用:返回文本字段中值的长

语法:

Select len ( column name) from table name;

PSMySQL 中函数为 length():

例子:计算class 表里面的name 列值的长度

Select len (name) as LengthOfName from class;

Round( ) 函数

作用:用于把数值字段舍入为指定的小数位

语法

Select round ( column name, decimals) from table name;

参数

描述

column name

必需。要舍入的字段

decimals

必需。规定要返回的小数位数。

例子:sale 表里面的price列的值四舍五入为1位小数

Select round (price,1) as newPrice from sale;

拓展:

ROUND(X) 返回参数X的四舍五入的一个整

mysql> select ROUND(-1.23);
        -> -1

mysql> select ROUND(-1.58);
        -> -2

mysql> select ROUND(1.58);
        -> 2

ROUND(X,D) 返回参数X的四舍五入的有 D 位小数的一个数字。如果D0,结果将没有小数点或小数部分

mysql> select ROUND(1.298, 1);
        -> 1.3
mysql> select ROUND(1.298, 0);
        -> 1

PSROUND 返回值被变换为一个BIGINT!

Now( ) 函数

作用:返回当前系统的日期和时

语法: Select now( );

Format( ) 函数

作用: 用于对字段的显示进行格式

语法:

Select format ( column name,format) from table name;

参数

描述

column name

必需。要格式化的字

format

必需。规定格式

例子:从 class 表里选取 date 列并格式化为YYYY-MM-DD 的日

select format(date,'%Y-%m-%d') as newNate from class;

以上就是SQL函数的基础用法。

猜你喜欢

转载自blog.csdn.net/zz_moon/article/details/82013172