I use one table for categories and content.
Value of Categories in pid field is 0.
For example:
Example Table
--------------------------
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
--开发者_如何学Python------------------------
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
How to get all categories and 10 content items in one query?
To get the results of two separate queries in a single query use UNION ALL:
SELECT id, pid, name
FROM yourtable
WHERE pid = 0
UNION ALL
(
SELECT id, pid, name
FROM yourtable
WHERE pid <> 0
LIMIT 10
)
精彩评论