2011-01-15 12 views
2

Ich verwende eine Tabelle für Kategorien und Inhalt.Limit basierend auf Count

Wert von Kategorien in pid Feld ist 0.

Zum Beispiel:

Beispiel Tabelle

-------------------------- 
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 

Wie in einer Abfrage alle Kategorien und 10 Inhaltselemente zu bekommen?

+1

10 Inhalt Artikel pro Kategorie? oder insgesamt – andrewjs

+0

10 Inhaltselemente und alle Kategorien. – Mirgorod

+1

like (SELECT * FROM Tabelle WHERE pid! = 0 LIMIT 0,10) + (SELECT * FROM Tabelle WHERE pid = 0) ergibt 10 Inhaltselemente und alle Kategorien in Tabelle – Mirgorod

Antwort

5

Um die Ergebnisse von zwei getrennten Abfragen in einer einzigen Abfrage Verwendung UNION zu bekommen ALL:

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

Danke. Es ist was ich brauche. – Mirgorod