Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8613753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:57:34+00:00 2026-06-12T04:57:34+00:00

I have a category table cat_id, cat_name, parent_id, cat_slug and I have article table

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:57:35+00:00Added an answer on June 12, 2026 at 4:57 am

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my categories table I have: cat_id , cat_name , cat_slug . In the
I have these tables: category table: cat_id (PK) cat_name category_options table: option_id (PK) cat_id
I have the following MySql Table: -------------------------------------------- Table 'category' -------------------------------------------- category_id name parent_id 1
i have MySql Table name 'category', in that table i have id catname parent_id
I have four tables post ------------- post_id cat_id posts post_category ------------- cat_id cat_name users
In one table I have fields cat_id, cat_name the table I need to update
I have categories table: cat_id cat_name Than categories - posts relationship table cat_rel :
I have a problem with building multi level select box. I have category table
Lets suppose that I have a Category table with a column that holds the
I have three tables Category, Movies and RelCatMov Category-table categoryid, categoryName 1 thriller 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.