How to make multiple where clauses for the same row?

早过忘川 提交于 2020-04-30 08:45:08

问题


I want to make multiple Where / AND clauses for the same row: This is my request body

"sub_categories":[
        {
            "category_id":2
        },
        {
            "category_id":1
        }

    ]

This is my javascript code

var where = {}
if (subcategories != undefined) {
    subcategories.forEach(async (item) => {
        where['$subcategories.id$'] = item.category_id
    });
}

Expected query to produce :

SELECT * FROM TABLE where sub_categories .category_id = 1 AND sub_categories .category_id = 2

Questy that is given to me :

SELECT * FROM TABLE where sub_categories .category_id = 2 (Last one) 

Do I need to add something to the code in order to do this?


回答1:


try

var where = {}
if (subcategories != undefined && subcategories.length) {
   where['$and'] = []
    subcategories.forEach((item) => {
        where['$and'].push({
           '$subcategories.id$': item.category_id
        })
    });
}


来源:https://stackoverflow.com/questions/61308665/how-to-make-multiple-where-clauses-for-the-same-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!