José Nobre
José Nobre

Reputation: 5027

How to make multiple where clauses for the same row?

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?

Upvotes: 1

Views: 80

Answers (1)

Anatoly
Anatoly

Reputation: 22793

try

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

Upvotes: 2

Related Questions