Sequelize V6.20.1 MVC模式(三 | 基本條件查詢 )

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

本文檔分多個篇章,難易程度從低到高,學(xué)習(xí)此篇章之前,務(wù)必確保自己已經(jīng)掌握 node.js、express、es6語法、mysql等關(guān)系型數(shù)據(jù)庫的sql語法等

條件查詢 where【承接 Sequelize V6.20.1 MVC模式(二)-- 單表

無論你是通過 findAll/find 或批量 update/destroy 進(jìn)行查詢,都可以傳遞一個 where對象來過濾查詢.
where通常用 attribute:value 鍵值對獲取一個對象,其中 value可以是匹配等式的數(shù)據(jù)或其他運算符的鍵值對象.
也可以通過嵌套 orand 運算符 的集合來生成復(fù)雜的 AND/OR 條件.

*以下操作均在 User.Controller.js 中進(jìn)行

基本條件查詢
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
        }
    ]
}
操作符查詢

操作符是對某個字段的進(jìn)一步約束,可以有多個(對同一個字段的多個操作符會被轉(zhuǎn)化為 AND

import {Op} from 'sequelize' //通過Op調(diào)用對應(yīng)操作符
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的數(shù)據(jù)
                [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的數(shù)據(jù)
                [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就介紹的差不多了,接下來介紹其他的查詢方法

查詢一條數(shù)據(jù) 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
        })
    }
查詢并獲取數(shù)量 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條數(shù)據(jù)
        ]
    }
}

好了,到此為止,Sequelize V6.20.1 單表的增刪查改以及條件查詢就講完了,更多知識補(bǔ)充可以前往Sequelize 官方文檔 查詢,下一章節(jié)咱們介紹 Sequelize V6.20.1 中數(shù)據(jù)表的關(guān)系:一對一,下一章節(jié)再見!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容