根据您对@wadesworld 的评论/解释这是一个简单的设计,可能会为您指明正确的方向
我使结构尽可能简单,您可以使用更多列/详细信息来适应您的应用程序,并且我已经展示了一些如何查询数据的示例。
TABLE: USERS
ID | USERNAME | PASSWORD
TABLE: CATEGORIES
ID | CATEGORY_ID | NAME
TABLE: POSTS
ID | CATEGORY_ID | DESCRIPTION | USER_ID
// list all top level categories (We use NULL in categories.category_id to indicate it has no parents)
SELECT * FROM categories WHERE category_id IS NULL;
// list all sub categories for category_id = 2
SELECT * FROM categories WHERE category_id=2;
// list parent and child categories, for use in a html select
SELECT parent.id, parent.name, child.id, child.name
FROM categories AS parent
JOIN categories AS child ON child.category_id=parent.id
WHERE parent.category_id IS NULL
// insert a post into category_id=2 for user_id = 1
INSERT INTO posts (category_id, description, user_id)
VALUES ('2', 'test post', 1);
// select all posts by user with username=tom
SELECT * FROM users
JOIN posts ON posts.user_id=users.id
JOIN categories ON categories.id=posts.category_id
WHERE users.username='tom';
// count the number of posts made by tom in each category
SELECT categories.name, COUNT(*)
FROM users
JOIN posts ON posts.user_id=users.id
JOIN categories ON categories.id=posts.category_id
WHERE users.username='tom';
GROUP BY categories.name