【问题标题】:MySQL: Getting Posts from CategoriesMySQL:从类别中获取帖子
【发布时间】:2011-08-29 09:53:21
【问题描述】:

我正在努力学习 MySQL,所以我创建了一个小博客系统。

我在 MySQL 中有 3 个表:

posts

id    |  title      
----------------
1     |  Post Title 1         
2     |  Post Title 2  

categories

id    |  title          | parent
--------------------------------
10     |  category10    | 0 
11     |  category11    | 0
12     |  category12    | 10 

post_category_relations

id    |  post_id   |   category_id
----------------------------------
1     |  1         |   10
2     |  2         |   12
3     |  3         |   11

每个帖子可以有多个类别,它们的关系存储在 post_category_relations 中:

所以当我访问 index.php?category=10 时,我想获取与 category10 相关的每个帖子,包括来自其子文件夹 category12 的帖子。

我未完成的 PHP 代码段

$folder_id = $_GET["category"]; // Get Category ID from the URL
$sql = "SELECT * FROM posts 
          JOIN categories
          JOIN post_category_relations
        // And I don't really know what should I do here
        // because I need the child categories first, then the relations
        // then I can get the post too from the post_id of the relations
       ";
mysql_query($sql);

我知道这需要高级 MySQL 技能,但我们不胜感激!我已经在 PHP 中做了这个,但是我需要使用 4 个循环,这在 MySQL 中可能不是最好的方法,我只是还不知道如何:)

【问题讨论】:

  • 首先,如果您应该在执行查询之前转义您的 get 输入(以防止 SQL 注入):$folder_id = $_GET["category"];应该是:$folder_id = mysql_real_escape_string(stripslashes($_GET["category"]));
  • 我还建议您阅读@Denis 提供的文章。你会学到很多东西:)我自己读了好几遍。
  • 使用 MySQL 会话变量:explainextended.com/2009/09/29/…。另请参阅此问题以获取其他选项:stackoverflow.com/questions/4048151/…

标签: php mysql hierarchical-data database-relations


【解决方案1】:

我无法测试我的查询,但我相信类似

select * from posts,post_category_relations where post.id=post_category_relations.post_id and
post_category_relations.category_id in (select id from categories where id=? or parent=?)

就是你要找的。​​p>

【讨论】:

  • @Rifat 并且在问题中提到类别不在单深度树中?引用,“所以当我访问 index.php?category=10 时,我想获取与 category10 相关的每个帖子,包括其子文件夹 category12 中的帖子。”
  • 表格本身的结构是多级的吧?
  • @Rifat 表的结构支持多级类别层次结构....以及单级,以及2级层次结构。根据 OP 提供的信息,可以假设他的意思是单级(最简单的情况)。如果不是,他应该澄清一下。
【解决方案2】:

您可能会发现 Phillip Keller 的这些文章很有趣:

它们包含标签,但您的查询(即 category1 and category2category1 or category2,以及您尝试编写的查询)几乎相同。

另请参阅有关索引分层数据的讨论:Managing Hierarchical Data in MySQL

以及 SO 上与嵌套集、标签、类别等相关的大量线程。

【讨论】:

  • 在这种情况下不能使用标签,只能使用树(我假设带有路径)。所以 +1 用于分层链接,但你可以写更详细的答案。
  • 是的,我想。不过,无论是标签还是类别,多类别查询都非常相似:他的 post2cat 表将分解为文章中讨论的 post2tag 表。
【解决方案3】:

这是一个 SQL:

 # Take post from castegory $cat_id
    (SELECT P.* 
        FROM 
          posts P, post_category_relations PR 
    WHERE 
       PR.category_id = {$cat_id} AND PR.post_id = P.id
     )
    UNION
    # Take all post from $cat_id child categories
    (SELECT P.* 
        FROM 
          posts P, post_category_relations PR, categories C
    WHERE 
       PR.category_id = C.parent AND PR.post_id = P.id 
       AND C.id = {$cat_id}
     )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 2011-05-16
    • 2014-11-18
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    相关资源
    最近更新 更多