我正在尝试对我的模型进行搜索,我希望得到所有用户,其中email、firstname或lastname类似于搜索字符串,或者userssecondaryemails表(相关模型)包含类似于该字符串的电子邮件。
因为or语句的部分在不同的表中,所以这有点棘手,我只能找到其他stackoverflow答案来帮助我。
以下是我的问题(简化):
const companiesUsersParams = {
where: {
companyId: req.params.companyId,
roleId: role.id
},
include: [{
model: models.Users,
attributes: ['id', 'firstName', 'lastName', 'email'],
where: req.query.s ? {
[Op.or]: [{
firstName: {
[Op.like]: `%${req.query.s}%`
}
}, {
lastName: {
[Op.like]: `%${req.query.s}%`
}
}, {
email: {
[Op.like]: `%${req.query.s}%`
}
}, {
'$usersSecondaryEmails.email$': {
[Op.like]: `%${req.query.s}%`
}
}]
} : null,
include: [{
model: models.UsersSecondaryEmails,
attributes: ['id', 'email'],
as: 'usersSecondaryEmails'
}]
}]
}
什么时候 req.query.s
没有定义,查询按预期运行(没有或语句),所以我知道这不是我的关联的问题。
当我运行此查询时 req.query.s
我明白了
“on子句”中的未知列“userssecondaryemails.email”
下面是正在生成的sql(格式尽可能好):
SELECT `companiesUsers`.`id`,
`companiesUsers`.`company_id` AS `companyId`,
`companiesUsers`.`user_id` AS `userId`,
`companiesUsers`.`role_id` AS `roleId`,
`companiesUsers`.`created_at` AS `createdAt`,
`companiesUsers`.`updated_at` AS`updatedAt`,
`user`.`id` AS `user.id`,
`user`.`first_name` AS `user.firstName`,
`user`.`last_name` AS `user.lastName`,
`user`.`email` AS `user.email`,
`user->usersSecondaryEmails`.`id` AS `user.usersSecondaryEmails.id`,
`user->usersSecondaryEmails`.`email` AS
`user.usersSecondaryEmails.email`
FROM `CompaniesUsers` AS `companiesUsers`
INNER JOIN `Users` AS `user`
ON `companiesUsers`.`user_id` = `user`.`id` AND
(`user`.`first_name` LIKE '%bob%' OR
`user`.`last_name` LIKE '%bob%' OR
`user`.`email` LIKE '%bob%' OR
`usersSecondaryEmails`.`email` LIKE '%bob%')
LEFT OUTER JOIN `UsersSecondaryEmails` AS `user->usersSecondaryEmails`
ON `user`.`id` = `user->usersSecondaryEmails`.`user_id`
WHERE `companiesUsers`.`company_id` = '1'
AND `companiesUsers`.`role_id` = 20;
任何关于多表文档或sequelize中语句的建议或链接都会很好(我在文档中找不到任何这样高级的东西)。
1条答案
按热度按时间qij5mzcb1#
我不知道如何修改的续集代码
companiesUsersParams
,但您的最终查询应该如下所示,以获得所需的输出。这可能会帮助您重写续集代码。用户的左连接而不是内部连接。
你应该移到最后或最后一个选项
usersSecondaryEmails.email LIKE '%bob%'
到userssecondaryemails连接条件在where子句中,检查条件(userid的user表或userssecondaryemails中至少应存在一行)
SELECT companiesUsers.id, companiesUsers.company_id AS companyId, companiesUsers.user_id AS userId, companiesUsers.role_id AS roleId, companiesUsers.created_at AS createdAt, companiesUsers.updated_at ASupdatedAt, user.id AS user.id, user.first_name AS user.firstName, user.last_name AS user.lastName, user.email AS user.email, user->usersSecondaryEmails.id AS user.usersSecondaryEmails.id, user->usersSecondaryEmails.email AS user.usersSecondaryEmails.email FROM CompaniesUsers AS companiesUsers LEFT OUTER JOIN Users AS user ---- #1 ON companiesUsers.user_id = user.id AND (user.first_name LIKE '%bob%' OR user.last_name LIKE '%bob%' OR user.email LIKE '%bob%' OR) LEFT OUTER JOIN UsersSecondaryEmails AS user->usersSecondaryEmails ON companiesUsers.user_id = user->usersSecondaryEmails.user_id AND user->usersSecondaryEmails.email LIKE '%bob%' ---- #2 WHERE companiesUsers.company_id = '1' AND companiesUsers.role_id = 20 AND (user.email is Not null OR user->usersSecondaryEmails.user_id is Not Null); ---- #3