MySQL中的一些常用函数。
AVG 函数返回数值列的平均值。NULL 值不包括在计算中。

COUNT(列) 函数返回指定列的值的数目(NULL 不计入)。
COUNT(*) 函数返回表中的记录数。
COUNT(DISTINCT 列) 函数返回指定列的不同值的数目。


MAX 函数返回一列中的最大值。NULL 值不包括在计算中。
MIN 函数返回一列中的最小值。NULL 值不包括在计算中。

SUM 函数返回数值列的总数(总额)。

GROUP BY 语句用于结合聚合函数,根据一个或多个列对结果集进行分组。以上函数都是聚合函数。

HAVING 子句,在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与聚合函数一起使用。
where、聚合函数、having 在from后面的执行顺序:where>聚合函数>having
HAVING语句通常与GROUP BY语句联合使用,用来过滤由GROUP BY语句返回的记录集
例查找订单总金额少于 2000 的客户。

UCASE 函数把字段的值转换为大写。

LCASE 函数把字段的值转换为小写。
MID 函数用于从文本字段中提取字符。
语句:select MID(字段 ,开始位置 [,length] ) from 表名;
它有三个参数,其中length参数可选可不选,表示要返回的字符数,开始位置默认起始为1

ROUND 函数用于把数值字段舍入为指定的小数位数。
语句:select ROUND(字段,decimals) from 表名;
它有两个参数,decimals 规定要返回的小数位数。

NOW 函数返回当前的日期和时间。

网友评论