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子查询操作实例详解
下一篇: 服务器搭建