带条件的联接查询

Man*_*wal 2 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);
});
Run Code Online (Sandbox Code Playgroud)

其生成以下查询:

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';
           ^^^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

因此,它给我的错误: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';
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么???

Eva*_*oky 5

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

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