繁体   English   中英

如何使用NodeJS在多个位置编写子查询

[英]How to write subquery with multiple where in sequelize using NodeJS

我需要使用sequelize执行此查询。

select * from mysqlDB.songTable where
X in (SELECT X FROM movieDB4.songTable where Y like('%pencil%') and Z='title') and
Y='tam' and Z='language';

我尝试过这样。 但是会引发一些无效的value [object]错误。 请帮助解决该查询。

const tempSQL = sequelize.dialect.QueryGenerator.selectQuery('songTable',{
      attributes: ['X'],
      where: {
           Y: {$like: '%'+text[i]},
           Z: "content_title"
      }})
      .slice(0,-1); // to remove the ';' from the end of the SQL

  User.findAll({
      where: {
          X: {
               $in: sequelize.literal('(' + tempSQL + ')'),
               $and: {Y: lang.substring(0,3),
                      Z: 'language'}
          }
      } 
  })

您可以使用sequelize.query()执行原始查询。

return this.sequelize.query(`SELECT category_id, category_name from table_categories where category_id in (SELECT DISTINCT category_id from table_authorized_service_center_details where center_id in (SELECT center_id from table_authorized_service_center where brand_id ${condition}));`).then((results) => {
                if (results.length === 0) {
                    reply({status: true, categories: [], forceUpdate: request.pre.forceUpdate});
                } else {
                    reply({status: true, categories: results[0], forceUpdate: request.pre.forceUpdate});
                }
            }).catch((err) => {
                console.log(err);
                reply({status: false, message: "ISE"});
            });

暂无
暂无

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

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