简体   繁体   中英

Sequelize “WHERE” Clause in subqueries

I'am trying to build my query using sequelize, in the where clause I need to give the conditional value from my front-end so i did it like this :

getResults(req) {
            return parm
                .findAll({
                    attributes: [
                        sequelize.literal('DISTINCT "id"')
                    ],
                    where :  {
                        name: req.query.parm.replace(/"/g, '').split(',')
                    } ,            
                    raw: true
                });

        }

and it's working! but now I need to write a subquery including where clause: something like this :

SELECT tab1.name FROM
(SELECT name FROM "MYTABLE"
WHERE id = (value from the front-end) AND name IN (values from front-end)
) as tab1

Here is what i have tried :

    getTest(req) { 
if (req.query.parm != null) {
 return parm .sequelize.query('SELECT id FROM "table_base" where id = $mid ); ',
 { type: sequelize.QueryTypes.SELECT , 
   bind: { mid: [req.query.parm.replace(/"/g, '').split(',')] }} ); 
} 
},

i tried to use raw query and i tested the binding parameters but i get this error when i execute this testing query :

Executing (default): SELECT id FROM "table_base" where id = $1 ); 

The answer to your question is YES it is indeed possible! SQL can pretty much do anything even if you are using sequelize. If you write the subquery and it doesn't work just post it back here so people can take a look and debug. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM