【MySQL】SQL菜鸟教程(四)

发布于:2025-02-10 ⋅ 阅读:(80) ⋅ 点赞:(0)

17. AVG() 函数

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

17.1 语法

SELECT AVG(column_name)
FROM table_name

17.2 示例

“access_log” 表的数据:

aid site_id count date
1 1 45 2016-05-10
2 3 100 2016-05-13
3 1 230 2016-05-14
4 2 10 2016-05-14
5 5 205 2016-05-14
6 4 13 2016-05-15
7 3 220 2016-05-15
8 5 545 2016-05-16
9 3 201 2016-05-17

1.从 “access_log” 表的 “count” 列获取平均值:

SELECT AVG(count) AS CountAverage
FROM access_log

在这里插入图片描述
2.选择访问量高于平均访问量的 “site_id” 和 “count”:

SELECT site_id, count
FROM access_log
WHERE count > (
	SELECT AVG(count) FROM access_log)

在这里插入图片描述

18. COUNT()函数

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

18.1 语法

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

SELECT COUNT(column_name)
FROM table_name

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

SELECT COUNT(*)
FROM table_name

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

SELECT COUNT(DISTINCT column_name)
FROM table_name

18.2 示例

“access_log” 表的数据:

aid site_id count date
1 1 45 2016-05-10
2 3 100 2016-05-13
3 1 230 2016-05-14
4 2 10 2016-05-14
5 5 205 2016-05-14
6 4 13 2016-05-15
7 3 220 2016-05-15
8 5 545 2016-05-16
9 3 201 2016-05-17

1.计算 “access_log” 表中 “site_id”=3 的总访问量:

SELECT COUNT(count) AS nums
FROM access_log
WHERE site_id = 3

2.计算 “access_log” 表中总记录数:

SELECT COUNT(*) AS nums
FROM access_log

3.计算 “access_log” 表中不同 site_id 的记录数:

SELECT COUNT(DISTINCT site_id) AS nums
FROM access_log

19. MAX() 函数

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

19.1 语法

SELECT MAX(clomun_name)
FROM table_name

19.2 示例

“Websites” 表的数据:

id name url alexa country
1 Google https://www.google.cm/ 1 USA
2 淘宝 https://www.taobao.com/ 13 CN
3 菜鸟教程 http://www.runoob.com/ 5000 CN
4 微博 http://weibo.com/ 20 CN
5 Facebook https://www.facebook.com/ 3 USA
6 百度 https://www.baidu.com/ 4 CN
7 stackoverflow http://stackoverflow.com/ 0 IND

1.从 “Websites” 表的 “alexa” 列获取最大值:

SELECT MAX(alexa) AS max_alexa
FROM Websites

20. MIN() 函数

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

20.1 语法

SELECT MIN(clomun_name)
FROM table_name

20.2 示例

“Websites” 表的数据:

id name url alexa country
1 Google https://www.google.cm/ 1 USA
2 淘宝 https://www.taobao.com/ 13 CN
3 菜鸟教程 http://www.runoob.com/ 5000 CN
4 微博 http://weibo.com/ 20 CN
5 Facebook https://www.facebook.com/ 3 USA
6 百度 https://www.baidu.com/ 4 CN
7 stackoverflow http://stackoverflow.com/ 0 IND

1.从 “Websites” 表的 “alexa” 列获取最小值:

SELECT MIN(alexa) AS min_alexa
FROM Websites

21. SUM()函数

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

21.1 语法

SELECT SUM(column_name)
FROM table_name

20.2 示例

“access_log” 表的数据:

aid site_id count date
1 1 45 2016-05-10
2 3 100 2016-05-13
3 1 230 2016-05-14
4 2 10 2016-05-14
5 5 205 2016-05-14
6 4 13 2016-05-15
7 3 220 2016-05-15
8 5 545 2016-05-16
9 3 201 2016-05-17

1.查找 “access_log” 表的 “count” 字段的总数:

SELECT SUM(count) AS nums
FROM access_log

在这里插入图片描述

22. GROUP BY

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

22.1 语法

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

22.2 示例

“Websites” 表的数据:

id name url alexa country
1 Google https://www.google.cm/ 1 USA
2 淘宝 https://www.taobao.com/ 13 CN
3 菜鸟教程 http://www.runoob.com/ 5000 CN
4 微博 http://weibo.com/ 20 CN
5 Facebook https://www.facebook.com/ 3 USA
6 百度 https://www.baidu.com/ 4 CN
7 stackoverflow http://stackoverflow.com/ 0 IND

“access_log” 表的数据:

aid site_id count date
1 1 45 2016-05-10
2 3 100 2016-05-13
3 1 230 2016-05-14
4 2 10 2016-05-14
5 5 205 2016-05-14
6 4 13 2016-05-15
7 3 220 2016-05-15
8 5 545 2016-05-16
9 3 201 2016-05-17

1.统计 access_log 各个 site_id 的访问量:

SELECT site_id, SUM(count) AS nums
FROM access_log
GROUP BY site_id

在这里插入图片描述
2.统计有记录的网站的记录数量:

SELECT W.name, COUNT(A.aid) AS nums
FROM access_log AS A
LEFT JOIN Websites AS W
ON A.site_id = W.id
GROUP BY W.name

在这里插入图片描述

23. HAVING

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

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

23.1 语法

SELECT column_name1, aggregate_function(column_name2)
FROM table_name
GROUP BY column_name1
HAVING condition

参数说明:

  • column1:要检索的列。
  • aggregate_function(column2):一个聚合函数,例如SUM、COUNT、AVG等,应用于column2的值。
  • table_name:要从中检索数据的表。
  • GROUP BY column1:根据column1列的值对数据进行分组。
  • HAVING condition:一个条件,用于筛选分组的结果。只有满足条件的分组会包含在结果集中。

23.2 示例

“Websites” 表的数据:

id name url alexa country
1 Google https://www.google.cm/ 1 USA
2 淘宝 https://www.taobao.com/ 13 CN
3 菜鸟教程 http://www.runoob.com/ 5000 CN
4 微博 http://weibo.com/ 20 CN
5 Facebook https://www.facebook.com/ 3 USA
6 百度 https://www.baidu.com/ 4 CN
7 stackoverflow http://stackoverflow.com/ 0 IND

“access_log” 表的数据:

aid site_id count date
1 1 45 2016-05-10
2 3 100 2016-05-13
3 1 230 2016-05-14
4 2 10 2016-05-14
5 5 205 2016-05-14
6 4 13 2016-05-15
7 3 220 2016-05-15
8 5 545 2016-05-16
9 3 201 2016-05-17

1.查找总访问量大于 200 的网站:

SELECT W.name, W.url, SUM(A.count) AS nums
FROM access_log AS A
INNER JOIN Websites AS W
GROUP BY W.name
HAVING SUM(A.count) > 200

在这里插入图片描述
2.查找总访问量大于 200 的网站,并且 alexa 排名小于 200:

SELECT W.name, W.url, SUM(A.count) AS nums
FROM access_log AS A
INNER JOIN Websites AS W
WHERE W.alexa < 200
GROUP BY W.name
HAVING SUM(A.count) > 200

在这里插入图片描述

where在group by前, having在group by 之后;
聚合函数(avg、sum、max、min、count),不能作为条件放在where之后,但可以放在having之后

24. EXISTS

EXISTS 运算符用于判断查询子句是否有记录,如果有一条或多条记录存在返回 True,否则返回 False。

24.1 语法

SELECT column_name 
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition)

24.2 示例

“Websites” 表的数据:

id name url alexa country
1 Google https://www.google.cm/ 1 USA
2 淘宝 https://www.taobao.com/ 13 CN
3 菜鸟教程 http://www.runoob.com/ 5000 CN
4 微博 http://weibo.com/ 20 CN
5 Facebook https://www.facebook.com/ 3 USA
6 百度 https://www.baidu.com/ 4 CN
7 stackoverflow http://stackoverflow.com/ 0 IND

“access_log” 表的数据:

aid site_id count date
1 1 45 2016-05-10
2 3 100 2016-05-13
3 1 230 2016-05-14
4 2 10 2016-05-14
5 5 205 2016-05-14
6 4 13 2016-05-15
7 3 220 2016-05-15
8 5 545 2016-05-16
9 3 201 2016-05-17

1.查找总访问量(count 字段)大于 200 的网站是否存在:

SELECT W.name, W.url
FROM Websites AS W
WHERE EXISTS (
	SELECT count FROM access_log AS A
	WHERE W.id = A.site_id AND count > 200)

在这里插入图片描述

25. UCASE()函数

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

25.1 语法

SELECT UCASE(column_name )
FROM table_name

25.2 示例

“Websites” 表的数据:

id name url alexa country
1 Google https://www.google.cm/ 1 USA
2 淘宝 https://www.taobao.com/ 13 CN
3 菜鸟教程 http://www.runoob.com/ 5000 CN
4 微博 http://weibo.com/ 20 CN
5 Facebook https://www.facebook.com/ 3 USA
6 百度 https://www.baidu.com/ 4 CN
7 stackoverflow http://stackoverflow.com/ 0 IND

1.从 “Websites” 表中选取 “name” 和 “url” 列,并把 “name” 列的值转换为大写:

SELECT UCASE(name) AS site_title, url
FROM Websites

在这里插入图片描述

26. LCASE()函数

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

26.1 语法

SELECT LCASE(column_name )
FROM table_name

26.2 示例

“Websites” 表的数据:

id name url alexa country
1 Google https://www.google.cm/ 1 USA
2 淘宝 https://www.taobao.com/ 13 CN
3 菜鸟教程 http://www.runoob.com/ 5000 CN
4 微博 http://weibo.com/ 20 CN
5 Facebook https://www.facebook.com/ 3 USA
6 百度 https://www.baidu.com/ 4 CN
7 stackoverflow http://stackoverflow.com/ 0 IND

1.从 “Websites” 表中选取 “name” 和 “url” 列,并把 “name” 列的值转换为小写:

SELECT LCASE(name) AS site_title, url
FROM Websites

在这里插入图片描述

27. MID()函数

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

27.1 语法

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

参数说明:

  • column_name : 要提取字符的字段。
  • start : 规定开始位置(起始值是 1)。
  • length : 可选。要返回的字符数。如果省略,则 MID() 函数返回剩余文本。

27.2 示例

“Websites” 表的数据:

id name url alexa country
1 Google https://www.google.cm/ 1 USA
2 淘宝 https://www.taobao.com/ 13 CN
3 菜鸟教程 http://www.runoob.com/ 5000 CN
4 微博 http://weibo.com/ 20 CN
5 Facebook https://www.facebook.com/ 3 USA
6 百度 https://www.baidu.com/ 4 CN
7 stackoverflow http://stackoverflow.com/ 0 IND

1.从 “Websites” 表的 “name” 列中提取前 4 个字符:

SELECT MID(name, 1, 4) AS ShortTitle
FROM Websites

在这里插入图片描述

28. LENGTH()函数

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

28.1 语法

SELECT LENGTH(column_name)
FROM table_name

28.2 示例

“Websites” 表的数据:

id name url alexa country
1 Google https://www.google.cm/ 1 USA
2 淘宝 https://www.taobao.com/ 13 CN
3 菜鸟教程 http://www.runoob.com/ 5000 CN
4 微博 http://weibo.com/ 20 CN
5 Facebook https://www.facebook.com/ 3 USA
6 百度 https://www.baidu.com/ 4 CN
7 stackoverflow http://stackoverflow.com/ 0 IND

1.从 “Websites” 表中选取 “name” 和 “url” 列中值的长度:

SELECT LENGTH(name) AS name_length, LENGTH(url) AS url_length
FROM Websites;

29. ROUND()函数

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

29.1 语法

SELECT ROUND(column_name, decimals)
FROM table_name

参数说明:

  • column_name:必需。要舍入的字段。
  • decimals:可选。规定要返回的小数位数。

29.2 示例

1.ROUND(X):返回参数X的四舍五入的一个整数。

SELECT ROUND(-1.23)

输出为 -1

2.ROUND(X,D): 返回参数X的四舍五入的有 D 位小数的一个数字。如果D为0,结果将没有小数点或小数部分。
一个整数。

SELECT ROUND(1.298, 1)
SELECT ROUND(1.298, 0)

输出为 1.3 和 1

30. FORMAT()函数

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

30.1 语法

SELECT FORMAT(column_name, format)
FROM table_name

参数说明:

  • column_name:必需。要舍入的字段。
  • format:必需。规定格式。

30.2 示例

“Websites” 表的数据:

id name url alexa country
1 Google https://www.google.cm/ 1 USA
2 淘宝 https://www.taobao.com/ 13 CN
3 菜鸟教程 http://www.runoob.com/ 5000 CN
4 微博 http://weibo.com/ 20 CN
5 Facebook https://www.facebook.com/ 3 USA
6 百度 https://www.baidu.com/ 4 CN
7 stackoverflow http://stackoverflow.com/ 0 IND

1.从 “Websites” 表中选取 name, url 以及格式化为 YYYY-MM-DD 的日期:

SELECT name, url, DATE_FORMAT(Now(), '%Y-%m-%d') AS date
FROM Websites

在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到