SQL 拥有很多可用于计数和计算的内建函数
SELECT function(列) FROM 表
AVG 函数返回数值列的平均值。NULL 值不包括在计算中
SELECT AVG(column_name) FROM table_name
“Orders” 表:
O_Id | OrderDate | OrderPrice | Customer |
---|---|---|---|
1 | 2008/12/29 | 1000 | Bush |
2 | 2008/11/23 | 1600 | Carter |
3 | 2008/10/05 | 700 | Bush |
4 | 2008/09/28 | 300 | Bush |
5 | 2008/08/06 | 2000 | Adams |
6 | 2008/07/21 | 100 | Carter |
计算 “OrderPrice” 字段的平均值,可以使用以下SQL语句:
SELECT AVG(OrderPrice) AS OrderAverage FROM Orders
结果集:
orderAverage |
---|
950 |
找到 OrderPrice 值高于 OrderPrice 平均值的客户,使用如下 SQL 语句:
SELECT Customer FROM Orders
WHERE OrderPrice>(SELECT AVG(OrderPrice) FROM Orders)
结果集:
Customer |
---|
Bush |
Carter |
Adams |
COUNT() 函数返回匹配指定条件的行数
COUNT(column_name) 函数返回指定列的值的数目(NULL 不计入):
SELECT COUNT(column_name) FROM table_name
COUNT(*) 函数返回表中的记录数:
SELECT COUNT(*) FROM table_name
COUNT(DISTINCT column_name) 函数返回指定列的不同值的数目:
SELECT COUNT(DISTINCT column_name) FROM table_name
注释:COUNT(DISTINCT) 适用于 ORACLE 和 Microsoft SQL Server,但是无法用于 Microsoft Access
“Orders” 表:
O_Id | OrderDate | OrderPrice | Customer |
---|---|---|---|
1 | 2008/12/29 | 1000 | Bush |
2 | 2008/11/23 | 1600 | Carter |
3 | 2008/10/05 | 700 | Bush |
4 | 2008/09/28 | 300 | Bush |
5 | 2008/08/06 | 2000 | Adams |
6 | 2008/07/21 | 100 | Carter |
计算客户 “Carter” 的订单数,使用如下 SQL 语句:
SELECT COUNT(Customer) AS CustomerNilsen FROM Orders
WHERE Customer='Carter'
CustomerNilsen |
---|
2 |
如果我们省略 WHERE 子句,比如这样:
SELECT COUNT(*) AS NumberOfOrders FROM Orders
NumberofOrders |
---|
6 |
计算 “Orders” 表中不同客户的数目,使用如下 SQL 语句:
SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders
NumberOfCustomers |
---|
3 |
FIRST() 函数返回指定的字段中第一个记录的值,且可使用 ORDER BY 语句对记录进行排序
SELECT FIRST(column_name) FROM table_name
“Orders” 表:
O_Id | OrderDate | OrderPrice | Customer |
---|---|---|---|
1 | 2008/12/29 | 1000 | Bush |
2 | 2008/11/23 | 1600 | Carter |
3 | 2008/10/05 | 700 | Bush |
4 | 2008/09/28 | 300 | Bush |
5 | 2008/08/06 | 2000 | Adams |
6 | 2008/07/21 | 100 | Carter |
希望查找 “OrderPrice” 列的第一个值,可以使用如下 SQL 语句:
SELECT FIRST(OrderPrice) AS FirstOrderPrice FROM Orders
结果集:
FirstOrderPrice |
---|
1000 |
LAST() 函数返回指定的字段中最后一个记录的值,且可使用 ORDER BY 语句对记录进行排序。
SELECT LAST(column_name) FROM table_name
“Orders” 表:
O_Id | OrderDate | OrderPrice | Customer |
---|---|---|---|
1 | 2008/12/29 | 1000 | Bush |
2 | 2008/11/23 | 1600 | Carter |
3 | 2008/10/05 | 700 | Bush |
4 | 2008/09/28 | 300 | Bush |
5 | 2008/08/06 | 2000 | Adams |
6 | 2008/07/21 | 100 | Carter |
希望查找 “OrderPrice” 列的最后一个值,可以使用如下 SQL 语句:
SELECT LAST(OrderPrice) AS LastOrderPrice FROM Orders
结果集:
LastOrderPrice |
---|
100 |
MAX 函数返回一列中的最大值。NULL 值不包括在计算中
SELECT MAX(column_name) FROM table_name
注释:MIN 和 MAX 也可用于文本列,以获得按字母顺序排列的最高或最低值。
“Orders” 表:
O_Id | OrderDate | OrderPrice | Customer |
---|---|---|---|
1 | 2008/12/29 | 1000 | Bush |
2 | 2008/11/23 | 1600 | Carter |
3 | 2008/10/05 | 700 | Bush |
4 | 2008/09/28 | 300 | Bush |
5 | 2008/08/06 | 2000 | Adams |
6 | 2008/07/21 | 100 | Carter |
希望查找 “OrderPrice” 列的最大值,可以使用如下 SQL 语句:
SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders
结果集:
LargestOrderPrice |
---|
2000 |
MIN 函数返回一列中的最小值。NULL 值不包括在计算中
SELECT MIN(column_name) FROM table_name
注释:MIN 和 MAX 也可用于文本列,以获得按字母顺序排列的最高或最低值。
“Orders” 表:
O_Id | OrderDate | OrderPrice | Customer |
---|---|---|---|
1 | 2008/12/29 | 1000 | Bush |
2 | 2008/11/23 | 1600 | Carter |
3 | 2008/10/05 | 700 | Bush |
4 | 2008/09/28 | 300 | Bush |
5 | 2008/08/06 | 2000 | Adams |
6 | 2008/07/21 | 100 | Carter |
希望查找 “OrderPrice” 列的最小值,可以使用如下 SQL 语句:
SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders
结果集:
SmallestOrderPrice |
---|
100 |
SUM 函数返回数值列的总数(总额)
SELECT SUM(column_name) FROM table_name
“Orders” 表:
O_Id | OrderDate | OrderPrice | Customer |
---|---|---|---|
1 | 2008/12/29 | 1000 | Bush |
2 | 2008/11/23 | 1600 | Carter |
3 | 2008/10/05 | 700 | Bush |
4 | 2008/09/28 | 300 | Bush |
5 | 2008/08/06 | 2000 | Adams |
6 | 2008/07/21 | 100 | Carter |
希望查找 “OrderPrice” 字段的总数,可以使用如下 SQL 语句:
SELECT SUM(OrderPrice) AS OrderTotal FROM Orders
结果集:
OrderTotal |
---|
5700 |
GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
“Orders” 表:
O_Id | OrderDate | OrderPrice | Customer |
---|---|---|---|
1 | 2008/12/29 | 1000 | Bush |
2 | 2008/11/23 | 1600 | Carter |
3 | 2008/10/05 | 700 | Bush |
4 | 2008/09/28 | 300 | Bush |
5 | 2008/08/06 | 2000 | Adams |
6 | 2008/07/21 | 100 | Carter |
希望查找每个客户的总金额(总订单),想要使用 GROUP BY 语句对客户进行组合,可以使用下列 SQL 语句:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
结果集:
Customer | SUM(OrderPrice) |
---|---|
Bush | 2000 |
Carter | 1700 |
Adams | 2000 |
如果省略GROUP BY语句
SELECT Customer,SUM(OrderPrice) FROM Orders
结果集会是:
Customer | SUM(OrderPrice) |
---|---|
Bush | 5700 |
Carter | 5700 |
Bush | 5700 |
Bush | 5700 |
Adams | 5700 |
Carter | 5700 |
上面的 SELECT 语句指定了两列(Customer 和 SUM(OrderPrice))。“SUM(OrderPrice)” 返回一个单独的值(“OrderPrice” 列的总计),而 “Customer” 返回 6 个值(每个值对应 “Orders” 表中的每一行)。因此,我们得不到正确的结果。不过,您已经看到了,GROUP BY 语句解决了这个问题
SELECT Customer,OrderDate,SUM(OrderPrice) FROM Orders
GROUP BY Customer,OrderDate
在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用
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
“Orders” 表:
O_Id | OrderDate | OrderPrice | Customer |
---|---|---|---|
1 | 2008/12/29 | 1000 | Bush |
2 | 2008/11/23 | 1600 | Carter |
3 | 2008/10/05 | 700 | Bush |
4 | 2008/09/28 | 300 | Bush |
5 | 2008/08/06 | 2000 | Adams |
6 | 2008/07/21 | 100 | Carter |
希望查找订单总金额少于 2000 的客户,可以使用如下 SQL 语句:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000
结果集:
Customer | SUM(orderPrice) |
---|---|
Carter | 1700 |
希望查找客户 “Bush” 或 “Adams” 拥有超过 1500 的订单总金额,在 SQL 语句中增加了一个普通的 WHERE 子句:
SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Bush' OR Customer='Adams'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500
结果集:
Customer | SUM(OrderPrice) |
---|---|
Bush | 2000 |
Adams | 2000 |
UCASE 函数把字段的值转换为大写
SELECT UCASE(column_name) FROM table_name
“Persons” 表:
Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Adams | John | oxford Street | London |
2 | Bush | George | Fifth Avenue | New York |
3 | Carter | Thomas | Changan Street | Beijing |
希望选取 “LastName” 和 “FirstName” 列的内容,然后把 “LastName” 列转换为大写
SELECT UCASE(LastName) as LastName,FirstName FROM Persons
结果集:
LastName | FirstName |
---|---|
ADAMS | John |
BUSH | George |
CARTER | Thomas |
LCASE 函数把字段的值转换为小写
SELECT LCASE(column_name) FROM table_name
“Persons” 表:
Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Adams | John | oxford Street | London |
2 | Bush | George | Fifth Avenue | New York |
3 | Carter | Thomas | Changan Street | Beijing |
希望选取 “LastName” 和 “FirstName” 列的内容,然后把 “LastName” 列转换为小写
SELECT LCASE(LastName) as LastName,FirstName FROM Persons
结果集:
LastName | FirstName |
---|---|
adams | John |
bush | George |
carter | Thomas |
MID 函数用于从文本字段中提取字符
SELECT MID(column_name,start[,length]) FROM table_name
参数 | 描述 |
---|---|
column_name | 必须,要提取字符的字段 |
start | 必须,规定开始位置(起始值是1) |
length | 可选,要返回的字符数,如果省略,则MID()函数返回剩余文本 |
“Persons” 表:
Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Adams | John | oxford Street | London |
2 | Bush | George | Fifth Avenue | New York |
3 | Carter | Thomas | Changan Street | Beijing |
从 “City” 列中提取前 3 个字符,可以使用如下 SQL 语句:
SELECT MID(City,1,3) as SmallCity FROM Persons
结果集:
SmallCity |
---|
Lon |
New |
Bei |
LEN 函数返回文本字段中值的长度
SELECT LEN(column_name) FROM table_name
“Persons” 表:
Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Adams | John | oxford Street | London |
2 | Bush | George | Fifth Avenue | New York |
3 | Carter | Thomas | Changan Street | Beijing |
要取得 “City” 列中值的长度,可以使用如下 SQL 语句:
SELECT LEN(City) as LengthOfCity FROM Persons
结果集:
lengthOfCity |
---|
6 |
8 |
7 |
ROUND 函数用于把数值字段舍入为指定的小数位数
SELECT ROUND(column_name,decimals) FROM table_name
参数 | 描述 |
---|---|
column_name | 必须,要舍入的字段 |
decimals | 必须,规定要返回的小数位数 |
“Products” 表:
Prod_Id | ProductName | Unit | UnitPrice |
---|---|---|---|
1 | gold | 1000 g | 32.35 |
2 | silver | 1000 g | 11.56 |
3 | copper | 1000 g | 6.85 |
把名称和价格舍入为最接近的整数,可以使用如下 SQL 语句:
SELECT ProductName, ROUND(UnitPrice,0) as UnitPrice FROM Products
结果集:
ProductName | UnitPrice |
---|---|
gold | 32 |
silver | 12 |
copper | 7 |
NOW 函数返回当前的日期和时间。
提示:如果您在使用 Sql Server 数据库,请使用 getdate() 函数来获得当前的日期时间
SELECT NOW() FROM table_name
“Products” 表:
Prod_Id | ProductName | Unit | UnitPrice |
---|---|---|---|
1 | gold | 1000 g | 32.35 |
2 | silver | 1000 g | 11.56 |
3 | copper | 1000 g | 6.85 |
要显示当天的日期所对应的名称和价格,使用如下 SQL 语句:
SELECT ProductName, UnitPrice, Now() as PerDate FROM Products
结果集:
ProductName | UnitPrice | PerDate |
---|---|---|
gold | 32.35 | 12/29/2008 11:36:00 AM |
silver | 11.56 | 12/29/2008 11:36:00 AM |
copper | 6.85 | 12/29/2008 11:36:00 AM |
FORMAT 函数用于对字段的显示进行格式化
SELECT FORMAT(column_name,format) FROM table_name
参数 | 描述 |
---|---|
column_name | 必须,要格式化的字段 |
format | 必须,规定格式 |
“Products” 表:
Prod_Id | ProductName | Unit | UnitPrice |
---|---|---|---|
1 | gold | 1000 g | 32.35 |
2 | silver | 1000 g | 11.56 |
3 | copper | 1000 g | 6.85 |
要显示每天日期所对应的名称和价格(日期的显示格式是 “YYYY-MM-DD”)可以使用如下 SQL 语句:
SELECT ProductName, UnitPrice, FORMAT(Now(),'YYYY-MM-DD') as PerDate
FROM Products
结果集:
ProductName | UnitPrice | PerDate |
---|---|---|
gold | 32.35 | 12/29/2008 |
silver | 11.56 | 12/29/2008 |
copper | 6.85 | 12/29/2008 |