码迷,mamicode.com
首页 > 其他好文 > 详细

第十三章:分组数据

时间:2019-11-07 19:17:59      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:having   cts   使用   创建   code   anti   sel   HERE   auth   

@author: Tobin
@date: 2019/10/28 22:32:57

关注GROUP BY和 HAVING子句

# 创建分组
SELECT vend_id , COUNT(*) AS num_prods
FROM products
GROUP BY vend_id;
#SELECT中,除了聚集语句之外,每个列都需要在GROUP BY语句中给出。
# GROUP BY 位于WHERE语句之后,ORDER BY语句之前。

# HAVING过滤分组,WHERE过滤行
SELECT vend_id , COUNT(*) AS num_prods
FROM products
GROUP BY vend_id
HAVING COUNT(*)>=2;

SELECT vend_id , COUNT(*) AS num_prods
FROM products
WHERE prod_price >=10
GROUP BY vend_id
HAVING COUNT(*)>=2;

# 使用ORDER BY 排序
SELECT order_num, SUM(quantity*item_price) AS ordertotal
FROM orderitems
GROUP BY order_num
HAVING SUM(quantity*item_price) >=50
ORDER BY ordertotal;

第十三章:分组数据

标签:having   cts   使用   创建   code   anti   sel   HERE   auth   

原文地址:https://www.cnblogs.com/zuotongbin/p/11814155.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!