SQL函数

SQL函数

来源:https://www.runoob.com/sql/sql-function.html

SQL Aggregate 函数

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

有用的 Aggregate 函数:

  • AVG() - 返回平均值
  • COUNT() - 返回行数
  • FIRST() - 返回第一个记录的值
  • LAST() - 返回最后一个记录的值
  • MAX() - 返回最大值
  • MIN() - 返回最小值
  • SUM() - 返回总和

SQL Scalar 函数

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

有用的 Scalar 函数:

  • UCASE() - 将某个字段转换为大写
  • LCASE() - 将某个字段转换为小写
  • MID() - 从某个文本字段提取字符,MySql 中使用
  • SubString(字段,1,end) - 从某个文本字段提取字符
  • LEN() - 返回某个文本字段的长度
  • ROUND() - 对某个数值字段进行指定小数位数的四舍五入
  • NOW() - 返回当前的系统日期和时间
  • FORMAT() - 格式化某个字段的显示方式

AVG() 函数

AVG() 函数返回数值列的平均值。

SQL AVG() 语法

SELECT AVG(column_name) FROM table_name;

mysql> select * from test9;
+-----+---------+------+------------+
| aid | site_id | cout | date       |
+-----+---------+------+------------+
|   2 |       1 |   45 | 2019-10-10 |
|   4 |       1 |   45 | 2016-05-10 |
|   6 |       2 |    5 | 2016-05-20 |
|   7 |       3 |   55 | 2017-05-20 |
|   8 |       4 |   45 | 2017-08-20 |
+-----+---------+------+------------+
5 rows in set (0.00 sec)
mysql> 
mysql> select avg(cout) from test9;
+-----------+
| avg(cout) |
+-----------+
|   39.0000 |
+-----------+
1 row in set (0.00 sec)

mysql> 

COUNT() 函数

COUNT() 函数返回匹配指定条件的行数。

SQL COUNT(column_name) 语法

COUNT(column_name) 函数返回指定列的值的数目(NULL 不计入):

SELECT COUNT(column_name) FROM table_name;

SQL COUNT(*) 语法

COUNT(*) 函数返回表中的记录数:

SELECT COUNT(*) FROM table_name; 

SQL COUNT(DISTINCT column_name) 语法

COUNT(DISTINCT column_name) 函数返回指定列的不同值的数目:

SELECT COUNT(DISTINCT column_name) FROM table_name;

注释:COUNT(DISTINCT) 适用于 ORACLE 和 Microsoft SQL Server,但是无法用于 Microsoft Access。

mysql> select avg(cout) from test9;
+-----------+
| avg(cout) |
+-----------+
|   39.0000 |
+-----------+
1 row in set (0.00 sec)

mysql> 
mysql> select count(cout) from test9;
+-------------+
| count(cout) |
+-------------+
|           5 |
+-------------+
1 row in set (0.00 sec)

mysql> select count(*) from test9;
+----------+
| count(*) |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)

mysql> select count(distinct site_id) from test9;
+-------------------------+
| count(distinct site_id) |
+-------------------------+
|                       4 |
+-------------------------+
1 row in set (0.00 sec)

mysql> 

 

 

FIRST() 函数

FIRST() 函数返回指定的列中第一个记录的值。

SQL FIRST() 语法

SELECT FIRST(column_name) FROM table_name;

注释:只有 MS Access 支持 FIRST() 函数。

MySQL 语法

SELECT column_name FROM table_name
ORDER BY column_name ASC
LIMIT 1;
 
mysql> select date from test9 order by date limit 1;
+------------+
| date       |
+------------+
| 2016-05-10 |
+------------+
1 row in set (0.00 sec)

mysql> 

 

 

LAST() 函数

LAST() 函数返回指定的列中最后一个记录的值。

SQL LAST() 语法

SELECT LAST(column_name) FROM table_name;

注释:只有 MS Access 支持 LAST() 函数。

 

MySQL 语法

SELECT column_name FROM table_name
ORDER BY column_name DESC
LIMIT 1;

 

mysql> select date from test9 order by date desc limit 1;
+------------+
| date       |
+------------+
| 2019-10-10 |
+------------+
1 row in set (0.00 sec)

mysql> 

 

 

MAX() 函数

MAX() 函数返回指定列的最大值。

SQL MAX() 语法

SELECT MAX(column_name) FROM table_name;
 
mysql> select max(cout) from test9;
+-----------+
| max(cout) |
+-----------+
|        55 |
+-----------+
1 row in set (0.00 sec)

mysql> 

 

 

MIN() 函数

MIN() 函数返回指定列的最小值。

SQL MIN() 语法

SELECT MIN(column_name) FROM table_name;
mysql> select min(cout) from test9;
+-----------+
| min(cout) |
+-----------+
|         5 |
+-----------+
1 row in set (0.00 sec)

mysql> 

 

SUM() 函数

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

SQL SUM() 语法

SELECT SUM(column_name) FROM table_name;

 

mysql> select sum(cout) from test9;
+-----------+
| sum(cout) |
+-----------+
|       195 |
+-----------+
1 row in set (0.00 sec)

mysql> 

 

 

GROUP BY 语句

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

SQL GROUP BY 语法

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

 统计各个 site_id 的访问量:

mysql> select site_id , sum(cout) as sum_cout from test9 group by site_id;
+---------+----------+
| site_id | sum_cout |
+---------+----------+
|       1 |       90 |
|       2 |        5 |
|       3 |       55 |
|       4 |       45 |
+---------+----------+
4 rows in set (0.00 sec)

mysql> 

 

 

HAVING 子句

在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与聚合函数一起使用。

HAVING 子句可以让我们筛选分组后的各组数据。

SQL HAVING 语法

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;

  统计各个 site_id 的访问量,且访问量>50

mysql> select site_id , sum(cout) as sum_cout from test9 group by site_id having sum(cout) > 50;
+---------+----------+
| site_id | sum_cout |
+---------+----------+
|       1 |       90 |
|       3 |       55 |
+---------+----------+
2 rows in set (0.02 sec)

mysql> 

 

 

UCASE() 函数

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

SQL UCASE() 语法

SELECT UCASE(column_name) FROM table_name;

用于 SQL Server 的语法

SELECT UPPER(column_name) FROM table_name;

 

mysql> select word, ucase(word) as ucase_word from test9;
+-------+------------+
| word  | ucase_word |
+-------+------------+
| abcd  | ABCD       |
| ABCD  | ABCD       |
| efGH  | EFGH       |
| EFgh  | EFGH       |
| Wrold | WROLD      |
+-------+------------+
5 rows in set (0.00 sec)

mysql>

 

 

LCASE() 函数

LCASE() 函数把字段的值转换为小写。

SQL LCASE() 语法

SELECT LCASE(column_name) FROM table_name;

用于 SQL Server 的语法

SELECT LOWER(column_name) FROM table_name;

 

mysql> select word, lcase(word) as ucase_word from test9;
+-------+------------+
| word  | ucase_word |
+-------+------------+
| abcd  | abcd       |
| ABCD  | abcd       |
| efGH  | efgh       |
| EFgh  | efgh       |
| Wrold | wrold      |
+-------+------------+
5 rows in set (0.00 sec)

mysql> 

 

 

MID() 函数

MID() 函数用于从文本字段中提取字符。

SQL MID() 语法

SELECT MID(column_name,start[,length]) FROM table_name;

 

 

mysql> select word, mid(word,2,2) as mid_word from test9;
+-------+----------+
| word  | mid_word |
+-------+----------+
| abcd  | bc       |
| ABCD  | BC       |
| efGH  | fG       |
| EFgh  | Fg       |
| Wrold | ro       |
+-------+----------+
5 rows in set (0.00 sec)

mysql> 

 

 

LEN() 函数

LEN() 函数返回文本字段中值的长度。

SQL LEN() 语法

SELECT LEN(column_name) FROM table_name;

MySQL 中函数为 LENGTH():

SELECT LENGTH(column_name) FROM table_name;

 

mysql> select word, length(word) as len_word from test9;
+-------+----------+
| word  | len_word |
+-------+----------+
| abcd  |        4 |
| ABCD  |        4 |
| efGH  |        4 |
| EFgh  |        4 |
| Wrold |        5 |
+-------+----------+
5 rows in set (0.00 sec)

mysql> 

 

 

ROUND() 函数

ROUND() 函数用于把数值字段舍入为指定的小数位数。

SQL ROUND() 语法

SELECT ROUND(column_name,decimals) FROM table_name;

 

mysql> select round(-1.23);
+--------------+
| round(-1.23) |
+--------------+
|           -1 |
+--------------+
1 row in set (0.00 sec)

mysql> 
mysql> 
mysql> select round(-1.68);
+--------------+
| round(-1.68) |
+--------------+
|           -2 |
+--------------+
1 row in set (0.00 sec)

mysql> 
mysql> select round(1.298, 1)
    -> ;
+-----------------+
| round(1.298, 1) |
+-----------------+
|             1.3 |
+-----------------+
1 row in set (0.00 sec)

mysql> select round(1.298, 0);
+-----------------+
| round(1.298, 0) |
+-----------------+
|               1 |
+-----------------+
1 row in set (0.00 sec)

mysql> 

 

 

NOW() 函数

NOW() 函数返回当前系统的日期和时间。

SQL NOW() 语法

SELECT NOW() FROM table_name;

 

mysql> select now() ;
+---------------------+
| now()               |
+---------------------+
| 2019-10-31 18:11:37 |
+---------------------+
1 row in set (0.00 sec)

mysql> 

 

 

FORMAT() 函数

FORMAT() 函数用于对字段的显示进行格式化。

SQL FORMAT() 语法

SELECT FORMAT(column_name,format) FROM table_name;

mysql> select date_format(now(),'%Y-%m-%d');
+-------------------------------+
| date_format(now(),'%Y-%m-%d') |
+-------------------------------+
| 2019-10-31                    |
+-------------------------------+
1 row in set (0.00 sec)

mysql> 

 

posted @ 2019-10-31 18:16  Justtosee  阅读(306)  评论(0编辑  收藏  举报