SQL函數

SQL函數

來源:https://www.runoob.com/sql/sql-function.htmlhtml

SQL Aggregate 函數

SQL Aggregate 函數計算從列中取得的值,返回一個單一的值。mysql

有用的 Aggregate 函數:sql

  • AVG() - 返回平均值
  • COUNT() - 返回行數
  • FIRST() - 返回第一個記錄的值
  • LAST() - 返回最後一個記錄的值
  • MAX() - 返回最大值
  • MIN() - 返回最小值
  • SUM() - 返回總和

SQL Scalar 函數

SQL Scalar 函數基於輸入值,返回一個單一的值。函數

有用的 Scalar 函數:spa

  • UCASE() - 將某個字段轉換爲大寫
  • LCASE() - 將某個字段轉換爲小寫
  • MID() - 從某個文本字段提取字符,MySql 中使用
  • SubString(字段,1,end) - 從某個文本字段提取字符
  • LEN() - 返回某個文本字段的長度
  • ROUND() - 對某個數值字段進行指定小數位數的四捨五入
  • NOW() - 返回當前的系統日期和時間
  • FORMAT() - 格式化某個字段的顯示方式

AVG() 函數

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

SQL AVG() 語法

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() 函數

COUNT() 函數返回匹配指定條件的行數。htm

SQL COUNT(column_name) 語法

COUNT(column_name) 函數返回指定列的值的數目(NULL 不計入):blog

SELECT COUNT(column_name) FROM table_name;ci

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> 
相關文章
相關標籤/搜索