Sequelize V6.20.1 MVC模式(三 | 基本条件查询 )

大家可以前往 Sequelize中文文档,查看 Sequelize不同版本【5.x、4.x】的文档

本文档分多个篇章,难易程度从低到高,学习此篇章之前,务必确保自己已经掌握 node.jsexpress、es6语法、mysql等关系型数据库的sql语法等

条件查询 where【承接 Sequelize V6.20.1 MVC模式(二)-- 单表

无论你是通过 findAll/find 或批量 update/destroy 进行查询,都可以传递一个 where对象来过滤查询.
where通常用 attribute:value 键值对获取一个对象,其中 value可以是匹配等式的数据或其他运算符的键值对象.
也可以通过嵌套 orand 运算符 的集合来生成复杂的 AND/OR 条件.

*以下操作均在 User.Controller.js 中进行

基本条件查询
getUserInfo: async (req, res, next)=>{
        let users = await User.findAll({
            where: {//获取id在[1,2,3]中并且age=20的
                id: [1,2,3], 
                age: 20
            },
            attributes: ['id', 'username', 'age'], //允许显示的字段
        });
        res.send({
            code: 200,
            users
        })
    }

postman

{
    "code": 200,
    "users": [
        {
            "id": 3,
            "username": "张三",
            "age": 20
        }
    ]
}
操作符查询

操作符是对某个字段的进一步约束,可以有多个(对同一个字段的多个操作符会被转化为 AND

import {Op} from 'sequelize' //通过Op调用对应操作符
getUserInfo: async (req, res, next)=>{
        let users = await User.findAll({
            where: {
                id: {
                    [Op.eq]: 1,                // id = 1
                    [Op.ne]: 2,                // id != 2
                    [Op.gt]: 6,                // id > 6
                    [Op.gte]: 6,               // id >= 6
                    [Op.lt]: 10,               // id < 10
                    [Op.lte]: 10,              // id <= 10
                    [Op.between]: [6, 10],     // id BETWEEN 6 AND 10
                    [Op.notBetween]: [11, 15], // id NOT BETWEEN 11 AND 15
                    [Op.in]: [1, 2],           // id IN (1, 2)
                    [Op.notIn]: [3, 4]         // id NOT IN (3, 4)
                },
                username: {
                    [Op.like]: '%m%',          // username LIKE '%m%'
                    [Op.notLike]: '%m'         // username NOT LIKE '%m'
                },
                updated_at: {
                    [Op.eq]: null,             // updated_at IS NULL
                    [Op.ne]: null              // updated_at IS NOT NULL
                }
            },
            attributes: ['id', 'username', 'age']
        });
        res.send({
            code: 200,
            users
        })
    }
AND条件
getUserInfo: async (req, res, next)=>{
        let users = await User.findAll({
            where: {//获取id在[7,8,9]中并且username中包含m的数据
                [Op.and]: [
                    {id: [7,8,9]},
                    {username: {
                        [Op.like]: '%M%'
                    }}
                ]
            },
            attributes: ['id', 'username', 'age']
        });
        res.send({
            code: 200,
            users
        })
    }
OR条件
getUserInfo: async (req, res, next)=>{
        let users = await User.findAll({
            where: {//获取id在[7,8,9]中或者username中包含m的数据
                [Op.or]: [
                    {id: [7,8,9]},
                    {username: {
                        [Op.like]: '%M%'
                    }}
                ]
            },
            attributes: ['id', 'username', 'age']
        });
        res.send({
            code: 200,
            users
        })
    }
排序 order
getUserInfo: async (req, res, next)=>{
        let users = await User.findAll({
            order: [
              ['id', 'DESC'] 
            ],
            attributes: ['id', 'username', 'age']
        });
        res.send({
            code: 200,
            users
        })
    }
分页查询 limit offset
getUserInfo: async (req, res, next)=>{
        let {offset, limit} = req.query;
        let users = await User.findAll({
            limit: parseInt(limit),
            offset: parseInt(offset),
            attributes: ['id', 'username', 'age']
        });
        res.send({
            code: 200,
            users
        })
    }

到此为止,条件查询 where就介绍的差不多了,接下来介绍其他的查询方法

查询一条数据 findOne
getUserInfo: async (req, res, next)=>{
        let {id} = req.query;
        let users = await User.findOne({
            where: {
                id: parseInt(id),
            },
            attributes: ['id', 'username', 'age']
        });
        res.send({
            code: 200,
            users
        })
    }
查询并获取数量 findAndCountAll
getUserInfo: async (req, res, next)=>{
        let users = await User.findAndCountAll({
            where: {
                // username包含李
                username: {
                    [Op.like]: '%m%'
                }
            },
            limit: 2,
            offset: 0,
            attributes: ['id', 'username', 'age']
        });
        res.send({
            code: 200,
            users
        })
    }

postman

{
    "code": 200,
    "users": {
        "count": 8,
        "rows": [
            {
                "id": 1,
                "username": "Mjhu",
                "age": 16
            }... //此处省略7条数据
        ]
    }
}

好了,到此为止,Sequelize V6.20.1 单表的增删查改以及条件查询就讲完了,更多知识补充可以前往Sequelize 官方文档 查询,下一章节咱们介绍 Sequelize V6.20.1 中数据表的关系:一对一,下一章节再见!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。