如何使用$ lte和别名?

时间:2019-01-22 15:18:44

标签: mysql node.js sequelize.js

我有一个带有别名的sequelize select查询,并且我只希望计数小于5的查询。

我已经有一个查询,该查询可以计数并正确进行联接。我真的只需要计数<5的部分。

Stock.findAndCountAll({
        attributes: [[Sequelize.fn('COUNT', Sequelize.col('*')),{stockcount: {$lt: 2}}, 'stockcount']],
        group: ['fk_product'],
        include: [{
            model: Product,
            as: 'product',
            attributes: {exclude: ['fk_creator']},
            include: [
                {
                    model: Category,
                    as: 'category'
                },
                {
                    model: User,
                    as: 'creator',
                    attributes: ["id", "username", "jabber", "email"]
                }
            ]
        }],
        where: {sold: 0, replace_unit: null}

目前我正在得到以下结果:

{
        "stockcount": 8,
        "product": {
            "id": 1,
            "title": "First Product",
            "description": "Some Description",
            "product_type": 0,
            "price": 9.99,
            "catId": 1,
            "rating": 0,
            "createdAt": "2019-01-19T22:56:43.000Z",
            "updatedAt": "2019-01-19T22:56:43.000Z",
            "category": {
                "id": 1,
                "catname": "First category",
                "icon": "fa-user",
                "createdAt": "2019-01-19T22:56:43.000Z",
                "updatedAt": "2019-01-19T22:56:43.000Z"
            },
            "creator": {
                "id": 1,
                "username": "testt1",
                "jabber": "test@test1.im",
                "email": "test@gun.de"
            }
        }
    },
    {
        "stockcount": 1,
        "product": {
            "id": 2,
            "title": "Second Product",
            "description": "Some Description",
            "product_type": 1,
            "price": 1.99,
            "catId": 1,
            "rating": 2,
            "createdAt": "2019-01-19T22:56:43.000Z",
            "updatedAt": "2019-01-19T22:56:43.000Z",
            "category": {
                "id": 1,
                "catname": "First category",
                "icon": "fa-user",
                "createdAt": "2019-01-19T22:56:43.000Z",
                "updatedAt": "2019-01-19T22:56:43.000Z"
            },
            "creator": {
                "id": 2,
                "username": "username",
                "jabber": "test2@test.im",
                "email": "test@test.de"
            }
        }
    }

如您所见,有一个字段“ stockcount”,这是一个别名。我的问题是,我无法使用它来使用Op.lte:5或类似的东西。谁能帮我? 预先感谢。

0 个答案:

没有答案