MySQL pivot table - 仅显示 A 的记录,但不在 B 的 ID 列表中
MySQL pivot table - show only records of A where not in list of ids in B
我有一个用户 table、一个类别 table 和一个 user_category table 作为支点。我想从 table 类别中选择多个 ID,然后生成一个查询,该查询仅显示不属于 selected 类别之一的用户的记录。
因此,如果用户属于两个类别,而我选择在 'NOT IN' 查询中排除其中一个类别,则它们不会出现在我的结果中。
这是我现在拥有的:
select distinct users.id, users.firstname, users.lastname, users.email from users INNER JOIN user_category ON users.id = user_category.user_id WHERE user_category.category_id NOT IN (280, 210, 177, 207, 240, 157, 187, 246, 153, 208, 199, 156, 281, 211, 212, 220, 218, 170, 201, 222, 236, 233)
问题是,如果用户属于类别 280 并且也是我不属于的类别之一 select,他们仍然会出现。
SELECT id, firstname, lastname, email
FROM users
WHERE id not in (
SELECT user_id
FROM user_category
WHERE category_id IN (280, 210, 177, 207, 240, 157, 187, 246, 153, 208, 199, 156, 281, 211, 212, 220, 218, 170, 201, 222, 236, 233)
)
我有一个用户 table、一个类别 table 和一个 user_category table 作为支点。我想从 table 类别中选择多个 ID,然后生成一个查询,该查询仅显示不属于 selected 类别之一的用户的记录。
因此,如果用户属于两个类别,而我选择在 'NOT IN' 查询中排除其中一个类别,则它们不会出现在我的结果中。
这是我现在拥有的:
select distinct users.id, users.firstname, users.lastname, users.email from users INNER JOIN user_category ON users.id = user_category.user_id WHERE user_category.category_id NOT IN (280, 210, 177, 207, 240, 157, 187, 246, 153, 208, 199, 156, 281, 211, 212, 220, 218, 170, 201, 222, 236, 233)
问题是,如果用户属于类别 280 并且也是我不属于的类别之一 select,他们仍然会出现。
SELECT id, firstname, lastname, email
FROM users
WHERE id not in (
SELECT user_id
FROM user_category
WHERE category_id IN (280, 210, 177, 207, 240, 157, 187, 246, 153, 208, 199, 156, 281, 211, 212, 220, 218, 170, 201, 222, 236, 233)
)