表1:用户 字段:id,email,first_name
表2:资源 字段:user_id,product_id
我想从用户表中选择电子邮件,first_name,其中用户有product_id 22但不是产品ID 1,3或35
提取此列表的SQL是什么?
答案 0 :(得分:1)
SELECT u.email,
u.first_name
FROM users u
JOIN resources r
ON u.id = r.user_id
WHERE product_id IN ( 1, 3, 22, 35 )
GROUP BY u.id,
u.email,
u.first_name
HAVING COUNT(DISTINCT product_id = 1)
AND MAX(product_id) = 22
答案 1 :(得分:1)
select email, first_name
from users
where id in (select user_id from resources where product_id=22)
and id not in (select user_id from resources where product_id IN (1,3,35))