來源:https://www.runoob.com/sql/sql-function.htmlhtml
SQL Aggregate 函數計算從列中取得的值,返回一個單一的值。mysql
有用的 Aggregate 函數:sql
SQL Scalar 函數基於輸入值,返回一個單一的值。函數
有用的 Scalar 函數:spa
AVG() 函數返回數值列的平均值。code
SELECT AVG(column_name) FROM table_name;orm
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() 函數返回匹配指定條件的行數。htm
COUNT(column_name) 函數返回指定列的值的數目(NULL 不計入):blog
SELECT COUNT(column_name) FROM table_name;ci
COUNT(*) 函數返回表中的記錄數:
SELECT COUNT(*) FROM table_name;
COUNT(DISTINCT column_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() 函數返回指定的列中第一個記錄的值。
註釋:只有 MS Access 支持 FIRST() 函數。
mysql> select date from test9 order by date limit 1; +------------+ | date | +------------+ | 2016-05-10 | +------------+ 1 row in set (0.00 sec) mysql>
LAST() 函數返回指定的列中最後一個記錄的值。
註釋:只有 MS Access 支持 LAST() 函數。
mysql> select date from test9 order by date desc limit 1; +------------+ | date | +------------+ | 2019-10-10 | +------------+ 1 row in set (0.00 sec) mysql>
MAX() 函數返回指定列的最大值。
mysql> select max(cout) from test9; +-----------+ | max(cout) | +-----------+ | 55 | +-----------+ 1 row in set (0.00 sec) mysql>
MIN() 函數返回指定列的最小值。
mysql> select min(cout) from test9; +-----------+ | min(cout) | +-----------+ | 5 | +-----------+ 1 row in set (0.00 sec) mysql>
SUM() 函數返回數值列的總數。
mysql> select sum(cout) from test9; +-----------+ | sum(cout) | +-----------+ | 195 | +-----------+ 1 row in set (0.00 sec) mysql>
GROUP BY 語句用於結合聚合函數,根據一個或多個列對結果集進行分組。
統計各個 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>
在 SQL 中增長 HAVING 子句緣由是,WHERE 關鍵字沒法與聚合函數一塊兒使用。
HAVING 子句能夠讓咱們篩選分組後的各組數據。
統計各個 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() 函數把字段的值轉換爲大寫。
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() 函數把字段的值轉換爲小寫。
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() 函數用於從文本字段中提取字符。
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() 函數返回文本字段中值的長度。
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() 函數用於把數值字段舍入爲指定的小數位數。
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() 函數返回當前系統的日期和時間。
mysql> select now() ; +---------------------+ | now() | +---------------------+ | 2019-10-31 18:11:37 | +---------------------+ 1 row in set (0.00 sec) mysql>
FORMAT() 函數用於對字段的顯示進行格式化。
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>