SQL函数

SQL 函数

为了数据的转换和处理方便,SQL提供了函数用来处理文本字符串、进行算术操作、处理日期和时间值、返回DBMS所使用的特殊信息。

一、文本处理函数

文本处理函数常用的大小写转换、清除空格、统计长度、返回SOUNDEX同音字符串等。
大写转换使用UPPER()函数:

mysql> select UPPER(cust_name) FROM Customers; +------------------+
| UPPER(cust_name) | +------------------+
| VILLAGE TOYS     |
| KIDS PLACE       |
| FUN4ALL          |
| FUN4ALL          |
| THE TOY STORE | +------------------+
5 rows in set (0.00 sec)

小写转换使用LOWER()函数:

select LOWER(cust_name) FROM Customers; 

SOUNDEX用于返回同音字符串,比如在Customers表中cust_contact列的名字为Michelle Green,如果输入错误Michael Green将会查不到结果:

mysql> SELECT cust_name,cust_contact FROM Customers  where cust_contact = 'Michael Green';
Empty set (0.00 sec)

使用SOUNDEX函数后:

mysql> SELECT cust_name,cust_contact FROM Customers where SOUNDEX(cust_contact) =SOUNDEX('Michael Green'); +------------+----------------+
| cust_name | cust_contact | +------------+----------------+
| Kids Place | Michelle Green | +------------+----------------+
1 row in set (0.00 sec)

二、时间和日期处理函数

提取日期年份(说明:不同的DBMS对日期处理函数的支持不一样,这里使用的是mysql):

查询2010年的订单:

mysql> select * from Orders WHERE YEAR(ORDER_DATE)=2010; +-----------+---------------------+------------+
| order_num | order_date | cust_id | +-----------+---------------------+------------+
|     20010 | 2010-05-27 00:00:00 | 1000000010 |
|     20011 | 2010-09-27 00:00:00 | 1000000011 |
|     20013 | 2010-11-12 00:00:00 | 1000000013 |
| 20015 | 2010-04-15 00:00:00 | 1000000015 | +-----------+---------------------+------------+
4 rows in set (0.01 sec)

三、数值处理函数

数值处理函数可以用户代数、三角或几何运算。

函数 说明
ABS() 返回一个数的绝对值
COS() 返回一个角度的余弦值
EXP() 返回一个数的指数值
PI() 返回圆周率
SIN() 返回一个角度的正弦值
SORT() 返回一个数的平方根
TAN() 返回一个角度的正切

四、聚集函数

聚集函数可以用来求和、求平均值、求最大最小值、统计行数、求标准差。
以Products表的pord_price列为例:

求和SUM():

mysql> select SUM(prod_price) AS SUM FROM Products; +-------+
| SUM | +-------+
| 61.41 | +-------+
1 row in set (0.00 sec)

求平均值AVG():

mysql> select AVG(prod_price) AS AVG FROM Products; +----------+
| AVG | +----------+
| 6.823333 | +----------+
1 row in set (0.00 sec)

求最大值MAX()

mysql> select MAX(prod_price) AS MAX_PRICE FROM Products; +-----------+
| MAX_PRICE | +-----------+
| 11.99 | +-----------+
1 row in set (0.00 sec)

求最小值MIN()

mysql> select MIN(prod_price) AS MIN_PRICE FROM Products; +-----------+
| MIN_PRICE | +-----------+
| 3.49 | +-----------+
1 row in set (0.01 sec)

统计行数COUNT()

mysql> select COUNT(*) AS TOTAL FROM PRODUCTS; +-------+
| TOTAL | +-------+
| 9 | +-------+
1 row in set (0.00 sec)

求标准差STD()

mysql> select STD(prod_price) AS STD FROM PRODUCTS; +----------+
| STD | +----------+
| 3.036811 | +----------+
1 row in set (0.00 sec)

组合使用聚集函数

select语句可以根据需求同时使用多个聚集函数:

mysql> select COUNT(*) AS 总个数,Max(prod_price) AS 最高价格 from Products; +-----------+--------------+
| 总个数 | 最高价格 | +-----------+--------------+
| 9 | 11.99 | +-----------+--------------+
1 row in set (0.00 sec)

使用GROUP BY分组

ORDER BY子句用于WHERE语句之后,ORDER BY子句之前。
以OrderItems表为列,先把OrderItems表列出来:

mysql> select * from OrderItems; +-----------+------------+---------+----------+------------+
| order_num | order_item | prod_id | quantity | item_price | +-----------+------------+---------+----------+------------+
|     20005 |          1 | BR01    |      100 |          5 |
|     20005 |          2 | BR03    |      100 |         11 |
|     20006 |          1 | BR01    |       20 |          6 |
|     20006 |          2 | BR02    |       10 |          9 |
|     20006 |          3 | BR03    |       10 |         12 |
|     20007 |          1 | BR03    |       50 |         11 |
|     20007 |          2 | BNBG01  |      100 |          3 |
|     20007 |          3 | BNBG02  |      100 |          3 |
|     20007 |          4 | BNBG03  |      100 |          3 |
|     20007 |          5 | RGAN01  |       50 |          4 |
|     20008 |          1 | RGAN01  |        5 |          5 |
|     20008 |          2 | BR03    |        5 |         12 |
|     20008 |          3 | BNBG01  |       10 |          3 |
|     20008 |          4 | BNBG02  |       10 |          3 |
|     20008 |          5 | BNBG03  |       10 |          3 |
|     20009 |          1 | BNBG01  |      250 |          2 |
|     20009 |          2 | BNBG02  |      250 |          2 |
| 20009 | 3 | BNBG03 | 250 | 2 | +-----------+------------+---------+----------+------------+
18 rows in set (0.00 sec)

如果要查出来prod_id为BNG01、BNG02。。。的各有多少个,可以使用GROUP BY,聚集的时候先分组数据,然后统计:

mysql> select prod_id,COUNT(*) AS prod_num from OrderItems group by prod_id; +---------+----------+
| prod_id | prod_num | +---------+----------+
| BNBG01  |        3 |
| BNBG02  |        3 |
| BNBG03  |        3 |
| BR01    |        2 |
| BR02    |        1 |
| BR03    |        4 |
| RGAN01 | 2 | +---------+----------+
7 rows in set (0.00 sec)

当需要引用一个条件,而该条件需要引用一个聚集函数的时候,使用HAVING子句,比如在上面的分组查询中,筛选个数大于2的:

mysql> select prod_id,COUNT(*) AS prod_num from OrderItems group by prod_id HAVING COUNT(*)>2; +---------+----------+
| prod_id | prod_num | +---------+----------+
| BNBG01  |        3 |
| BNBG02  |        3 |
| BNBG03  |        3 |
| BR03 | 4 | +---------+----------+
4 rows in set (0.00 sec)

你可能感兴趣的:(sql)