mysql - 如何使用NodeJS在sequelize中编写具有多个where的子查询

标签 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';

我尝试过这样的。但它会引发一些无效值[对象]错误。请帮忙解决这个问题。

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"});
            });

关于mysql - 如何使用NodeJS在sequelize中编写具有多个where的子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46300111/

相关文章:

mySQL - 仅从数据库中获取最高分

javascript - 使用discord.js,如何将 API 结果中不必要的部分切掉并发送?

javascript - NodeJS 和 Express、ENOENT 查看文件

node.js - 使用 for each in node js sequelize 将数据存储在数组中

mysql - Linux + MySQL : How to make outputs/responses visible?

php - Codeigniter 使用增量更新 mysql 数据

javascript - pool.query 不是函数 - 使用 npm mysql 包使用连接池

mysql - Sequelize 无法将 'belongsto' 和 'hasmany' 关联起来

mysql - 如何使用sequelize连接上传数据的两个表?

php - 我需要帮助更新特定用户的 sql 条目