doujinai2183 2012-09-27 14:44
浏览 29
已采纳

在主要类别PHP MYSQL中显示子类别的文章

I have a category table

cat_id, cat_name, parent_id, cat_slug

and I have article table as follow

art_id, cat_id, art_title, art_content, author_id .....

and my users table as

user_id, username, pw, .....

so with these data, I want to display all articles which are listed under sub category

for example:

url of main category is: mydomain.com/cat/business url of sub category is: mydomian.com/cat/business/advertising

so when people visit the main category, then it should display the subcategories listed articles on main category even though the cat_id is not mentioned as main category. I want to list out all sub categories of business's subcategories articles on business category page.

what is the query, i should try to achieve this

help will be appreciated. thanks

  • 写回答

2条回答 默认 最新

  • duanchi5078 2012-09-27 14:50
    关注

    First, to get your categories...

    If your category nesting is only two levels deep (parent and child) or you are only interested in direct sub-categories (i.e. not subcategories of subcategories), then your SQL is pretty simple:

    SELECT cat_id FROM category_table WHERE cat_id = $catid OR parent_id = $catid
    

    If your nesting is deeper, then you'll need to recursively query as far down as your category nesting goes. For example, if you limit yourself to 3 levels, you can do this with SQL like this:

    SELECT cat_id FROM category_table ct1
    WHERE cat_id = $catid OR parent_id = $catid
    OR parent_id in (SELECT cat_id FROM category_table WHERE parent_id = ct1.cat_id)
    

    Similarly, for a 4-level deep nesting, you can make the SQL even more complicated:

    SELECT cat_id FROM category_table ct1
    WHERE cat_id = $catid OR parent_id = $catid
    OR parent_id in (SELECT cat_id FROM category_table ct2 WHERE parent_id = ct1.cat_id
                     OR parent_id in
                     (SELECT cat_id FROM category_table ct3 WHERE parent_id = ct2.cat_id))
    

    And so on.

    If you do not have a fixed nesting depth, then you'll need to do the looping in the code.

    Once you have all categories, then you query articles. So for a simple 2-level dependency, you'll get

    SELECT * FROM article_table
    WHERE cat_id in (SELECT cat_id FROM category_table WHERE cat_id = $catid OR parent_id = $catid)
    

    Similarly, for a 3-level nesting, you'll get

    SELECT * FROM article_table
    WHERE cat_id in
        (SELECT cat_id FROM category_table ct1
         WHERE cat_id = $catid OR parent_id = $catid
         OR parent_id in (SELECT cat_id FROM category_table WHERE parent_id = ct1.cat_id))
    

    And so on...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据