2016-05-15 15 views
1

は、ここに私のテーブルです:カテゴリごとに正確な記事数を選択するにはどうすればよいですか?

CREATE TABLE IF NOT EXISTS `category` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `title` varchar(50) NOT NULL, 
    `post_limit` int(11) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ; 

INSERT INTO `category` (`id`, `title`, `post_limit`) VALUES 
(1, 'News', 2), 
(2, 'Sport', 2), 
(3, 'Science', 1), 
(4, 'Games', 1); 

CREATE TABLE IF NOT EXISTS `article` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `title` varchar(50) NOT NULL, 
    `category_id` int(11) NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `category_id` (`category_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ; 

INSERT INTO `article` (`id`, `title`, `category_id`) VALUES 
(1, 'news article 1', 1), 
(2, 'news article 2', 1), 
(3, 'news article 3', 1), 
(4, 'sports article 1', 2), 
(5, 'sports article 2', 2), 
(6, 'sports article 3', 2), 
(7, 'Science article 1', 3), 
(8, 'Science article 2', 3), 
(9, 'games article 1', 4), 
(10, 'games article 2', 4); 

私は何をする必要があることを選択10回の記事(ORDER BY article.id DESC)が、例えば、我々はpost_limit=2場合category_id=1 5の記事のために取ることができないので、すべてのカテゴリが、post_limitを持っ念頭に置いてあります。

ありがとうございます。

UPDATE 1: 結果は次のようになります。

10 games article 2  4 
8 science article 2 3 
6 sports article 3 2 
5 sports article 2 2 
3 news article 3  1 
+0

サンプルデータと希望の結果を提供できますか? –

+0

あなたは何を試してみることができますか? – halfer

+0

私はすぐにオンラインになる、私は現在モバイルになっている – CroiOS

答えて

1

post_limitを適用する記事を列挙する必要があると思います。あなたがサブクエリでこれを行うことができます。実際には

select a.* 
from (select a.*, 
      (@rn := if(@c = a.category_id, @rn + 1, 
         if(@c := a.category_id, 1, 1) 
         ) 
      ) as rn 
     from articles a cross join 
      (select @rn := 0, @c := -1) params 
     order by category_id 
    ) a join 
    category c 
    on c.id = a.category_id 
where a.rn <= c.post_limit 
limit 10; 

、あなたはおそらくlimitorder byは、記事をより詳細に制御を持っていると思います。同様に、サブクエリ内のorder byの別のキーで記事を制御したいとします。 。 。最新の記事を入手するにはorder by category_id, id descのようにしてください。

+0

あなたのソリューションを使用して+私が望むように正確な結果を得るために2つの変更を加えた:https://dl.dropboxusercontent。 com/u/77033905/ss/2016-05-15_2146.png – CroiOS

+0

あなたのソリューションは問題ありませんが、カテゴリの1つにpost_limit = 0またはpost_limit = NULLがある場合は1つの欠点があります。 – CroiOS

2

カテゴリのpost_limitに各カテゴリの数を制限するには:

select c.id, c.title, least(post_limit, count(a.id)) 
from category c 
left join article a on category_id = c.id 
group by 1, 2 -- or "group by c.id, c.title" if you prefer the verbose style 

SQLFiddleを参照してください。

1
select max(a.id) "Id", 
     max(a.title) "Title", 
     a.category_id "Category_Id, 
     max(c.id), 
     max(c.title), 
     count(*), 
     max(c.post_limit) 
from article a 
left outer join article b on b.category_id = a.category_id AND b.id>=a.id 
left outer join category c on a.category_id = c.id 
group by a.id 
having count(a.id) <= max(c.post_limit) 
order by max(a.id) desc 
limit 10 

これは、各記事(表a)に乗り、HIGHERのId値(表b)と同じカテゴリのすべての記事を検索します。

次に、記事ID(a.id)でグループ化し、カテゴリがpost_limitよりも小さい数のグループのみを選択します。

関連する問題