如果这是重复的话,我道歉,但是,我找不到我需要的答案。
考虑我的数据:
表格 - report_detail
report_id |category |sub_category |report name
-------------------------------------------------------
1 |1 |1 |Donkey Report
2 |2 |2 |Grandma Report
3 |1 |1 |Poop Report
表格 - report_subscriptions
user_id |report_id
--------------------------
1 |1
2 |2
1 |2
我的问题是,如何在report_subscriptions中从report_detail表中选择未订阅user_id = 1的所有report_id?
谢谢!
答案 0 :(得分:0)
这应该让你相当接近
select * from Report_detail RD
where RD.report_id not in (select disctinct RS.report_id from report_subscriptions RS)
答案 1 :(得分:0)
在SQL Fiddle测试:http://sqlfiddle.com/#!2/e7e3a/1/0
select rd.report_id
from report_detail rd
where not exists (
select 1
from report_subscriptions rs
where rs.user_id = 1
and rs.report_id = rd.report_id);
答案 2 :(得分:0)
如果您只需要报告ID,则根本没有理由获得report_detail表 只需:
SELECT DISTINCT report_id FROM report_subscriptions WHERE user_id <> 1
如果您确实需要使用此约束从report_details表中获取数据,只需执行
SELECT * FROM report_detail
WHERE report_id IN (SELECT DISTINCT report_id FROM report_subscriptions WHERE user_id <> 1)