2016-09-23 6 views
-1

私は、ユーザのフラグを立てるコンテンツのインスタンスを格納するSQLテーブルを持っています。これが私のテーブルです:テーブルにX回現れるレコードを返すSQLクエリ

Flagged Content 

1. instance_id 
2. content_id 
3. user_id` 

は今、私はflagged contentテーブル内で少なくとも3回表示されるすべてのcontent_idを返すクエリを実行します。しかし、私はどのように進めるか分からない。

+1

表示されていない。 –

+0

[count> 1のレコードを検索するためのSQLクエリの可能な重複](http://stackoverflow.com/questions/7151401/sql-query-finding-records-where-count-1) – Jeff

答えて

1

あなたはgroup byを使用してカウントを取得することができます

select content_id 
from flagged_content fc 
group by content_id 
having count(*) >= 3; 

あなたは、元のレコードをしたい場合は、joinを使用します。

select fc.* 
from flagged_content fc join 
    (select content_id 
     from flagged_content fc 
     group by content_id 
     having count(*) >= 3 
    ) c 
    on fc.content_id = c.content_id; 
1
SELECT content_id 
FROM flagged_content 
GROUP BY content_id 
HAVING COUNT(*) >= 3 
0

SELECTはCONTENT_ID BY flagged_content グループから をCONTENT_ID HAVING count(instance_id)> 3;

またはinstance_idを主キー/非NULL列に置き換えます。 (すべてのRDBMSでcount(*)が機能するかどうかは不明)

関連する問題