MySQL使用集合函数进行查询操作实例详解
2019/6/30 18:09:04
本文主要是介绍MySQL使用集合函数进行查询操作实例详解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文实例讲述了MySQL使用集合函数进行查询操作。分享给大家供大家参考,具体如下:
COUNT
函数
SELECT COUNT(*) AS cust_num from customers; SELECT COUNT(c_email) AS email_num FROM customers; SELECT o_num, COUNT(f_id) FROM orderitems GROUP BY o_num;
SUM
函数
SELECT SUM(quantity) AS items_total FROM orderitems WHERE o_num = 30005; SELECT o_num, SUM(quantity) AS items_total FROM orderitems GROUP BY o_num;
AVG
函数
SELECT AVG(f_price) AS avg_price FROM fruits WHERE s_id = 103; SELECT AVG(f_price) AS avg_price FROM fruits group by s_id;
MAX
函数
SELECT MAX(f_price) AS max_price FROM fruits; SELECT s_id, MAX(f_price) AS max_price FROM fruits GROUP BY s_id; SELECT MAX(f_name) from fruits;
MIN
函数
SELECT MIN(f_price) AS min_price FROM fruits; SELECT s_id, MIN(f_price) AS min_price FROM fruits GROUP BY s_id;
【例.34】查询customers表中总的行数
SELECT COUNT(*) AS cust_num from customers;
【例.35】查询customers表中有电子邮箱的顾客的总数,输入如下语句:
SELECT COUNT(c_email) AS email_num FROM customers;
【例.36】在orderitems表中,使用COUNT()
函数统计不同订单号中订购的水果种类
SELECT o_num, COUNT(f_id) FROM orderitems GROUP BY o_num;
【例.37】在orderitems表中查询30005号订单一共购买的水果总量,输入如下语句:
SELECT SUM(quantity) AS items_total FROM orderitems WHERE o_num = 30005;
【例.38】在orderitems表中,使用SUM()
函数统计不同订单号中订购的水果总量
SELECT o_num, SUM(quantity) AS items_total FROM orderitems GROUP BY o_num;
【例.39】在fruits表中,查询s_id=103的供应商的水果价格的平均值,SQL语句如下:
SELECT AVG(f_price) AS avg_price FROM fruits WHERE s_id = 103;
【例.40】在fruits表中,查询每一个供应商的水果价格的平均值,SQL语句如下:
SELECT s_id,AVG(f_price) AS avg_price FROM fruits GROUP BY s_id;
【例.41】在fruits表中查找市场上价格最高的水果,SQL语句如下:
mysql>SELECT MAX(f_price) AS max_price FROM fruits;
【例7.42】在fruits表中查找不同供应商提供的价格最高的水果
SELECT s_id, MAX(f_price) AS max_price FROM fruits GROUP BY s_id;
【例.43】在fruits表中查找f_name的最大值,SQL语句如下
SELECT MAX(f_name) from fruits;
【例.44】在fruits表中查找市场上价格最低的水果,SQL语句如下:
mysql>SELECT MIN(f_price) AS min_price FROM fruits;
【例.45】在fruits表中查找不同供应商提供的价格最低的水果
SELECT s_id, MIN(f_price) AS min_price FROM fruits GROUP BY s_id;
更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL常用函数大汇总》、《MySQL日志操作技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》及《MySQL数据库锁相关技巧汇总》
希望本文所述对大家MySQL数据库计有所帮助。
这篇关于MySQL使用集合函数进行查询操作实例详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-20部署MySQL集群入门:新手必读指南
- 2024-11-20部署MySQL集群教程:初学者指南
- 2024-11-20部署MySQL集群项目实战:新手教程
- 2024-11-20部署MySQL集群资料:新手入门教程
- 2024-11-20MySQL集群部署教程:入门级详解
- 2024-11-20MySQL集群教程:入门与实践指南
- 2024-11-20部署MySQL集群教程:新手入门指南
- 2024-11-20MySQL读写分离教程:轻松入门
- 2024-11-20部署MySQL集群入门:一步一步搭建你的数据库集群
- 2024-11-19部署MySQL集群学习:入门教程