0

これが重複している場合は申し訳ありませんが、必要な答えが見つかりません。

私のデータを考えてみましょう:

表 - 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 で user_id = 1 にサブスクライブされていない report_detail テーブルからすべての report_id を選択するにはどうすればよいですか?

ありがとうございました!

4

3 に答える 3

0

これでかなり近づくはずです

select * from Report_detail RD
where RD.report_id not in (select disctinct RS.report_id from report_subscriptions RS)
于 2012-11-29T23:41:50.680 に答える
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);
于 2012-11-29T23:44:55.560 に答える
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)
于 2012-11-29T23:45:07.523 に答える