2011-01-15 19 views
2

カテゴリとコンテンツに1つのテーブルを使用します。カウントに基づく制限

PIDフィールド内のカテゴリーの値は、例えば0

です:

例の表1のクエリですべてのカテゴリと10個のコンテンツアイテムを取得する方法

-------------------------- 
id | pid | name 
-------------------------- 
1 | 0 | Some catgory 1 
-------------------------- 
2 | 1 | Some content of first category 
-------------------------- 
3 | 1 | Other content of first cat 
-------------------------- 
4 | 0 | Second category 
-------------------------- 
5 | 0 | Category number 3 
-------------------------- 
6 | 5 | Content of category 3 
-------------------------- 
7 | 4 | Content of 2 cat 
-------------------------- 
8 | 5 | Content of 3 cat 
-------------------------- 
9 | 5 | Other Content of 3 cat 
-------------------------- 
10 | 5 | One more content of 3 cat 
-------------------------- 
11 | 4 | Content of 2 cat 
-------------------------- 
12 | 5 | One more content of 3 cat 
-------------------------- 
13 | 1 | First cat content 
-------------------------- 
14 | 1 | Other content of 1 cat 

?カテゴリごとの

SELECT id, pid, name 
FROM yourtable 
WHERE pid = 0 
UNION ALL 
(
    SELECT id, pid, name 
    FROM yourtable 
    WHERE pid <> 0 
    LIMIT 10 
) 
+1

10コンテンツ・アイテム: – andrewjs

+0

コンテンツ項目とすべてのカテゴリが含まれます。 – Mirgorod

+1

like(SELECT * FROM table WHERE pid!= 0 LIMIT 0,10)+(SELECT * FROM table WHERE pid = 0)結果は10個のコンテンツアイテムとなり、 – Mirgorod

答えて

5

はALL単一のクエリの使用UNIONにおける二つの別々のクエリの結果を取得するには?または合計で
+0

〜それは私が必要なものです。 – Mirgorod

関連する問題