[TOC]
### sequelize 查詢示例
~~~
const Op = Sequelize.Op
[Op.and]: {a: 5} // 且 (a = 5)
[Op.or]: [{a: 5}, {a: 6}] // (a = 5 或 a = 6)
[Op.gt]: 6, // id > 6
[Op.gte]: 6, // id >= 6
[Op.lt]: 10, // id < 10
[Op.lte]: 10, // id <= 10
[Op.ne]: 20, // id != 20
[Op.eq]: 3, // = 3
[Op.not]: true, // 不是 TRUE
[Op.between]: [6, 10], // 在 6 和 10 之間
[Op.notBetween]: [11, 15], // 不在 11 和 15 之間
[Op.in]: [1, 2], // 在 [1, 2] 之中
[Op.notIn]: [1, 2], // 不在 [1, 2] 之中
[Op.like]: '%hat', // 包含 '%hat'
[Op.notLike]: '%hat' // 不包含 '%hat'
[Op.iLike]: '%hat' // 包含 '%hat' (不區分大小寫) (僅限 PG)
[Op.notILike]: '%hat' // 不包含 '%hat' (僅限 PG)
[Op.startsWith]: 'hat' // 類似 'hat%'
[Op.endsWith]: 'hat' // 類似 '%hat'
[Op.substring]: 'hat' // 類似 '%hat%'
[Op.regexp]: '^[h|a|t]' // 匹配正則表達式/~ '^[h|a|t]' (僅限 MySQL/PG)
[Op.notRegexp]: '^[h|a|t]' // 不匹配正則表達式/!~ '^[h|a|t]' (僅限 MySQL/PG)
[Op.iRegexp]: '^[h|a|t]' // ~* '^[h|a|t]' (僅限 PG)
[Op.notIRegexp]: '^[h|a|t]' // !~* '^[h|a|t]' (僅限 PG)
[Op.like]: { [Op.any]: ['cat', 'hat']} // 包含任何數組['cat', 'hat'] - 同樣適用于 iLike 和 notLike
[Op.overlap]: [1, 2] // && [1, 2] (PG數組重疊運算符)
[Op.contains]: [1, 2] // @> [1, 2] (PG數組包含運算符)
[Op.contained]: [1, 2] // <@ [1, 2] (PG數組包含于運算符)
[Op.any]: [2,3] // 任何數組[2, 3]::INTEGER (僅限PG)
[Op.col]: 'user.organization_id' // = 'user'.'organization_id', 使用數據庫語言特定的列標識符, 本例使用 PG
~~~
數據表結構
| id | name | age | sex | address | math | english |
| --- | --- | --- | --- | --- | --- | --- |
| 張三 | 23 | 男 | 18 | 云南 | 60 | 40|
| 李四 | 23 | 女 | 22 | 貴州| 20 | 30|
| 王麻子 | 23 | 男 | 30| 玉溪 | 30 | 26|
| 趙六 | 23 | 女 | 26 | 德州 | 80 | 38 |
| 劉能 | 23 | 女 | 19 | 西涼 | 100| 48 |
| 段魚 | 23 | 男 | 28 | 廣州 | 60| 52|
| 琳娜 | 23 | 男 | 34 | 泰國 | 40| 46|
| 貝利 | 23 | 男 | 42 | 美國 | 50| 69 |
| 皮特 | 23 | 女 | 50 | 英國 | 38| 70 |
| 湯姆 | 23 | 男 | 46 | 緬甸 | 42| 65 |
*****
* [ ] 查詢年齡 > 20 的學生
~~~
model.findAll({
attributes: ['name', 'age'],
where: {
// age > 20
age: {
[Op.gt]:20
}
}
})
~~~
*****
* [ ] 查詢年齡 > 20 && < 30 的學生
~~~
model.findAll({
attributes: ['name', 'age'],
where: {
age: {
[Op.and]: {
[Op.gt]:20,
[Op.lt]:30
}
}
}
})
`student`.`age` > 20 AND `student`.`age` < 30
~~~
*****
* [ ] 查詢年齡 in 28 26 52
~~~
model.findAll({
attributes: ['name', 'age'],
where: {
age: {
[Op.in]: [26,28,52]
}
}
});
`student`.`age` IN (26, 28, 52)
~~~
*****
* [ ] 查詢英語成績為null的
~~~
model.findAll({
attributes:['name','english'],
where: {
english: {
[Op.is]:null
}
}
})
`student`.`english` IS NULL
~~~
*****
* [ ] 查詢英語成績 IS NOT NULL 不為空
~~~
Student.findAll({
attributes:['name','english'],
where: {
english: {
[Op.not]:null
}
}
})
`student`.`english` IS NOT NULL
~~~
- 概述
- 起步
- 跨域配置
- 路徑別名
- 路由
- 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類