Reputation: 1012
I have the following Accounts_Users table
:
account_id | user_id
--------------------
1 | 60
2 | 60
1 | 50
3 | 50
And I want to retrieve all the user_id's which do not have rows with certain account_id For example, if the account_id = 2 I want the result should be:
user_id
-------
50
Since user_id = 60 have record with account = 2.
my current query looks like this:
let existingUserIdsWithAccountUser = await AccountUserModel.findAll({
raw: true,
where: {
account_id: account_id,
user_id: {
[Sequelize.Op.in]: existingUsersIds
}
},
attributes: [Sequelize.fn('DISTINCT', Sequelize.col('user_id')), 'user_id']
}).map(user => user.user_id);
const existingUserIdsWithoutAccountUser = existingUsersIds.filter(user_id => !existingUserIdsWithAccountUser.includes(user_id));
I want to do a single query without having to filter the results.
I also tired the following:
let existingUserIdsWithoutAccountUser = await AccountUserModel.findAll({
raw: true,
where: {
account_id: {
[Sequelize.Op.not]: account_id
},
user_id: {
[Sequelize.Op.in]: existingUsersIds
}
},
attributes: [Sequelize.fn('DISTINCT', Sequelize.col('user_id')), 'user_id']
}).map(user => user.user_id);
but in this case, if I have a record with a different account_id then it still gets returned obviously.
Upvotes: 2
Views: 3012
Reputation: 1012
I was finally able to do it using the following query:
const existingUserIdsWithoutAccountUser = await AccountUserModel.findAll({
raw: true,
where: {
user_id: {
[Sequelize.Op.and]: {
[Sequelize.Op.notIn]: Sequelize.literal(`(SELECT user_id FROM \`Accounts_Users\` WHERE account_id = ${account_id})`),
[Sequelize.Op.in]: existingUsersIds
}
}
},
attributes: [Sequelize.fn('DISTINCT', Sequelize.col('user_id')), 'user_id']
}).map(user => user.user_id);
Upvotes: 2