mysql-6 数据检索(4)
2018-06-17 23:13:24来源:未知 阅读 ()
汇总数据
函数 |
说明 |
AVG() |
返回某列的平均数 |
COUNT() |
返回某列的行数 |
MAX() |
返回某列的最大值 |
MIN() |
返回某列的最小值 |
SUM() |
返回某列值的和 |
1、AVG函数
SELECT AVG(prod_price) AS avg_price FROM products;
SELECT AVG(prod_price) AS avg_price FROM products WHERE vend_id = 1003;
2、count()函数
SELECT COUNT(*) AS num_cust FROM customers;
SELECT COUNT(cust_email) AS num_cust FROM customers;
这条select语句使用count(cust_email)对cust_email列中有值的进行计数,在此例子中,cust_email的计算式3次,表示5个客户中只有三个客户有电子邮件
3、max()函数
SELECT MAX(prod_price) AS max_price FROM products;
4、min()函数
SELECT MIN(prod_price) AS min_price FROM products;
5、sum()函数
SELECT SUM(quantity) AS items_ordered FROM orderitems WHERE order_num = 20005;
SELECT SUM(item_price*quantity) AS total_price FROM orderitems WHERE order_num = 20005;
SELECT COUNT(*) AS num_items,MIN(prod_price) AS price_min, MAX(prod_price) AS price_max,AVG(prod_price) AS avg_price FROM products;
分组数据 group by
SELECT vend_id ,COUNT(*) AS num_prods FROM products GROUP BY vend_id;
SELECT cust_id ,COUNT(*) AS orders FROM orders GROUP BY cust_id HAVING COUNT(*)>=2;
SELECT prod_price,vend_id,COUNT(*) AS num_prods FROM products WHERE prod_price >=10 GROUP BY vend_id HAVING COUNT(*)>=2;
SELECT vend_id, COUNT(*) AS num_prods FROM products GROUP BY vend_id HAVING COUNT(*)>=2;
order by 1、排序产生的输出 2、任意列都可以使用 3、不一定需要
group by 1、分组行,但输出可能不是分组的顺序 2、只可能使用选择列或表达式列,而且必须使用每个选择列表达式 3、如果与聚集函数一起使用列,则必须使用
where过滤行,having过滤组
SELECT order_num ,SUM(quantity*item_price) AS ordertotal FROM orderitems GROUP BY order_num HAVING SUM(quantity*item_price)>=50;
SELECT order_num ,SUM(quantity*item_price) AS ordertotal FROM orderitems GROUP BY order_num HAVING SUM(quantity*item_price)>=50 ORDER BY ordertotal;
使用子查询
SELECT order_num FROM orderitems WHERE prod_id = 'TNT2'; SELECT cust_id FROM orders WHERE order_num IN (20005,20007); SELECT cust_id FROM orders WHERE order_num IN (SELECT order_num FROM orderitems WHERE prod_id = 'TNT2');
SELECT AVG(prod_price) *AS avg_price FROM products; SELECT AVG(prod_price) AS avg_price FROM products WHERE vend_id = 1003; SELECT COUNT(*) AS num_cust FROM customers; SELECT COUNT(cust_email) AS num_cust FROM customers; SELECT MAX(prod_price) AS max_price FROM products; SELECT MIN(prod_price) AS min_price FROM products; SELECT SUM(quantity) AS items_ordered FROM orderitems WHERE order_num = 20005; SELECT SUM(item_price*quantity) AS total_price FROM orderitems WHERE order_num = 20005; SELECT COUNT(*) AS num_items,MIN(prod_price) AS price_min, MAX(prod_price) AS price_max,AVG(prod_price) AS avg_price FROM products; SELECT vend_id ,COUNT(*) AS num_prods FROM products GROUP BY vend_id; SELECT cust_id FROM orders ; SELECT cust_id ,COUNT(*) AS orders FROM orders GROUP BY cust_id HAVING COUNT(*)>=2; SELECT prod_price,vend_id,COUNT(*) AS num_prods FROM products WHERE prod_price >=10 GROUP BY vend_id HAVING COUNT(*)>=2; SELECT vend_id, COUNT(*) AS num_prods FROM products GROUP BY vend_id HAVING COUNT(*)>=2; SELECT order_num ,SUM(quantity*item_price) AS ordertotal FROM orderitems GROUP BY order_num HAVING SUM(quantity*item_price)>=50; SELECT order_num ,SUM(quantity*item_price) AS ordertotal FROM orderitems GROUP BY order_num HAVING SUM(quantity*item_price)>=50 ORDER BY ordertotal; SELECT order_num FROM orderitems WHERE prod_id = 'TNT2'; SELECT cust_id FROM orders WHERE order_num IN (20005,20007); SELECT cust_id FROM orders WHERE order_num IN (SELECT order_num FROM orderitems WHERE prod_id = 'TNT2');
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:MySQL数据库的导入和导出
下一篇:MySQL数据库
- MySQL启动错误怎么办 2020-02-26
- Mysql支持的数据类型有哪些 2020-02-26
- HTML、JS与PHP之间的数据传输 2019-09-23
- MySQL性能优化的20条经验 2019-09-23
- TP5 paginate()分页后给结果集追加字段和数据 2019-09-17
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash