php–MySQL:从类别中获取帖子

php–MySQL:从类别中获取帖子,第1张

概述我正在尝试学习MySQL,所以我创建了一个小博客系统.我在MySQL中有3个表:帖子:id | title ---------------- 1 | Post Title 1 2 | Post Title 2 类别:id | title | parent --------

我正在尝试学习MySQL,所以我创建了一个小博客系统.

我在MysqL中有3个表:

帖子:

ID    |  Title      ----------------1     |  Post Title 1         2     |  Post Title 2  

类别:

ID    |  Title          | parent--------------------------------10     |  category10    | 0 11     |  category11    | 012     |  category12    | 10 

post_category_relations:

ID    |  post_ID   |   category_ID----------------------------------1     |  1         |   102     |  2         |   123     |  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中可能时,我只是不知道如何:)最佳答案您可能会发现Phillip Keller的这些文章很有趣:

> Tags: Database schemas
> Tagsystems: performance tests

它们涵盖了标签,但您的查询(即category1和category2与category1或category2以及您尝试编写的那些)几乎相同.

另见关于索引分层数据的讨论:Managing Hierarchical Data in MySQL.

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

总结

以上是内存溢出为你收集整理的php – MySQL:从类别中获取帖子全部内容,希望文章能够帮你解决php – MySQL:从类别中获取帖子所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1168579.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存