如何使用NodeJS在sequelize中编写多个子查询

时间:2017-09-19 12:10:32

标签: mysql node.js sequelize.js node-modules sequelize-cli

我需要使用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';

我试过这样的。但它会抛出一些无效的值[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'}
          }
      } 
  })

1 个答案:

答案 0 :(得分:0)

您可以使用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"});
            });