简体   繁体   English

在子查询中后缀“ WHERE”子句

[英]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 : 我正在尝试使用sequelize构建查询,在where子句中,我需要从前端提供条件值,所以我这样做是这样的:

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 : 但是现在我需要编写一个包含where子句的子查询:

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. 即使使用sequelize,SQL几乎可以做任何事情。 If you write the subquery and it doesn't work just post it back here so people can take a look and debug. 如果您编写了子查询,但该子查询不起作用,只需将其重新发布回此处,以便人们进行查看和调试。 Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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