join - 使用条件对连接查询进行序列化

标签 join sequelize.js

我使用以下语法来选择记录:

models.account.findAll({
    attributes: ['password'],
    include: [{
            model: models.user,
            as : 'user'
    }],
    where: {
        'password': password,
        'user.email': email
        }
}).then(function(res){
    console.log(res);
});

它生成以下查询:
SELECT * 
FROM   `accounts` AS `account` 
       LEFT OUTER JOIN `users` AS `user` 
                    ON `account`.`user_id` = `user`.`id` 
WHERE  `account`.`password` = 'PASSWORD' 
       AND `account`.`user.email` = 'xyz@gmail.com';
           ^^^^^^^^^^^^^^^^^^^^^^

所以它给了我错误:Unknown column 'account.user.email' in 'where clause' .我只想要 user.email .预期查询如下:
SELECT * 
    FROM   `accounts` AS `account` 
           LEFT OUTER JOIN `users` AS `user` 
                        ON `account`.`user_id` = `user`.`id` 
    WHERE  `account`.`password` = 'PASSWORD' 
           AND `user`.`email` = 'xyz@gmail.com';

我究竟做错了什么???

最佳答案

将用户表的 where 过滤器放在 include 部分:

models.account.findAll({
  attributes: ['password'],
  include: [{
    model: models.user,
    where: {
      email: email
    }
  }],
  where: {
    password: password
  }
}).then(function(res){
  console.log(res);
});

关于join - 使用条件对连接查询进行序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29892752/

相关文章:

javascript - Sequelize.js 一对多关系外键

mysql查询将三个查询连接成一个查询

mysql - 为什么我的删除内部联接不起作用?

node.js - 无服务器 lambda - 函数未完全执行

node.js - 第二级包括使用 sequelize

javascript - 无法获取/mentor/mypage/pre-ordered_Lectures/1

mysql - Sequelize 迁移失败,错误号 : 150 "Foreign key constraint is incorrectly formed"

php - 从多个表中获取图像

PHP Activerecord - 连接同一张表

mysql - 如何从创建虚拟列的表中获取不同的值 'types' - MySQL