[TOC]
### GROUP 分組查詢
>[danger] 分組查詢,顧名思義就是按照組區分開,進行查詢,比如按照性別分組
> 注意!!! group by 后面分組的字段不能隨便加,比如 group by 'sex' 那么,查詢字段必須為 sex
### 按照性別分組查詢全部數學成績的平均分
~~~
const { Sequelize } = app;
// 按照性別分組查詢全班數學成績的平均分
const ret = await Student.findAll({
// 查詢字段要和分組字段一致!!!!
attributes:['sex', [ Sequelize.fn('AVG', Sequelize.col('math')), 'math_avg' ] ],
// 分組字段
group: ['sex']
})
SELECT `sex`, AVG(`math`) FROM `student` GROUP BY `sex`;
[
{
"sex": "女",
"math_avg": "85.4286"
},
{
"sex": "男",
"math_avg": "78.0000"
}
]
~~~
*****
### 按照性別分組,分別查詢全班同學數學成績平均分和總人數
~~~
// 按照性別分組,分別查詢全班同學數學成績平均分和總人數
const ret = await Student.findAll( {
attributes:[
'sex',
[Sequelize.fn('AVG', Sequelize.col('math')), 'match_avg'],
[Sequelize.fn('COUNT', Sequelize.col('id')), 'total_student']
],
group: ['sex']
} )
SELECT `sex`, AVG(`math`) , COUNT(`id`) FROM `student` GROUP BY `sex`;
[
{
"sex": "女",
"match_avg": "85.4286",
"total_student": 7
},
{
"sex": "男",
"match_avg": "78.0000",
"total_student": 5
}
]
~~~
*****
### 按照地區分組,分別統計每個地區數學成績平均分 和 總人數,分數低于70分的同學不參與分組
~~~
// 按照地區分組,分別統計每個地區數學成績平均分 和 總人數,分數低于70分的同學不參與分組
const ret = await Student.findAll( {
attributes:[
'address',
[ Sequelize.fn('AVG', Sequelize.col('math')), 'math_avg' ],
[ Sequelize.fn('COUNT', Sequelize.col('id')), 'total_student' ]
],
where: {
math: {
[Op.gt]:70
}
},
group: ['address']
} )
SELECT `address`, AVG(`math`) , COUNT(`id`) FROM `student` WHERE `math` > 70 GROUP BY `address`;
[
{
"address": "四川",
"math_avg": "84.0000",
"total_student": 2
},
{
"address": "昆明",
"math_avg": "90.0000",
"total_student": 1
},
{
"address": "潭州",
"math_avg": "92.0000",
"total_student": 1
},
{
"address": "西涼",
"math_avg": "82.0000",
"total_student": 2
},
{
"address": "貴州",
"math_avg": "86.2500",
"total_student": 4
}
]
~~~
*****
### 按照地區分組,分別統計每個地區學生的數學平均分,分數低于70分的同學不參與分組,并且參與分組的人數不能少于2人
>[danger] 注意!!!where 和 having 的區別?
> where 在分組之前進行限定,如果不滿足條件,則不參與分組
> having 在分組之后進行限定,如果不滿足結果,則不會被查詢出來
> where 后不可以帶上聚合函數判斷,having 后可以帶上聚合函數進行判斷
~~~
await Student.findAll( {
attributes:[
'address',
[Sequelize.fn('AVG', Sequelize.col('math')), 'math'],
[Sequelize.fn('COUNT', Sequelize.col('id')), 'total']
],
where: {
math: {
[Op.gt]:70
}
},
group: 'address',
having: {
total:{
[Op.gt]:2
}
}
})
SELECT `address`, AVG(`math`),COUNT(`id`) as total FROM `student`
WHERE `student` > 70
GROUP BY `address`
HAVING `total` > 2;
~~~
- 概述
- 起步
- 跨域配置
- 路徑別名
- 路由
- api版本控制
- 錯誤和異常
- 全局異常處理
- 數據庫
- 創建遷移文件
- sequelize數據類型
- 配置
- 新增
- 查詢
- 條件查詢
- 模糊查詢
- 排序查詢
- 聚合查詢
- 分組查詢
- 分頁查詢
- 修改
- 刪除
- 獲取器
- 修改器
- 靜態屬性
- 字段驗證
- 外鍵約束
- 關聯模型
- 一對一
- 一對多
- 左外連接
- 多對多
- 字段顯示隱藏
- 事務
- 字段自增
- 驗證層
- egg-validate
- indicative驗證器
- egg-validate-plus
- betterValidate
- 校驗規則
- 中間件
- 安全
- 數據加密
- 單向加密
- 示例代碼
- 封裝egg加密
- 上傳
- path模塊
- 單文件上傳
- 多文件上傳
- 按照日期存儲
- 工具函數
- egg常用工具函數
- 緩存
- 配置緩存插件
- 設置緩存
- 獲取緩存
- 刪除緩存
- 消息隊列
- rabbitMQ
- 安裝
- 簡單隊列
- 工作隊列
- 工作隊列(dispach分發)
- 消息應答和持久化
- redis
- 數據類型
- 字符串類型(String)
- 哈希類型(Hash)
- 列表(List)
- 無序集合(Set)
- 可排序集合(Zset)
- 郵件系統
- nodeMailer
- 第三方模塊
- 生成隨機數
- JWT
- JWT鑒權
- 生成Token
- 短信服務
- 阿里大魚短信驗證碼
- 發送短信邏輯
- 阿里短信Node類