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 6796791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:31:49+00:00 2026-05-26T18:31:49+00:00

I have a cat table that has the following columns: cat_id | name |

  • 0

I have a cat table that has the following columns:

cat_id  | name            |   parent_id
1         cat1                1
2         subcat1             1
3         subcat1-subcat      2

This table has thousands of categories but that is the general structure.

When a user selects a top level category I have a query to get its children like this:

SELECT * FROM cat WHERE parent = $id

My problem is that I need to know if these children categories have children of their own.

I could do a loop on the results and do a query for each category returned, but I am hoping that there is a solution where I can use just one query, maybe it will require a sub query?

Thanks for the help.

  • 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-05-26T18:31:49+00:00Added an answer on May 26, 2026 at 6:31 pm

    You could use a subquery to check if there are child nodes:

    select  *
    ,       case 
            when exists (select * from cat c2 where c2.parent_id = c1.cat_id) then 0
            else 1
            end as HasSubcategories
    from    cat c1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some tables that has the following structure: cat( cat_id(pk), cat_name) ven( ven_id(pk),
I have a simple cakephp app with table articles that has a cat_id column
Say I have a table animals that has a column parent_id Say also that
I have a table in a db that has a structure like this: Date
I have a table that looks like this: id, col1, col2 col3 0 Goat
I have the following table: categories: cat_id | cat_cat | cat_name 1 | 0
Say I have a table that has items and attributes listed like, frog green
I have an existing table structure that looks something like this: AnimalTable ------------- |Id
This has been driving me mad. I have three tables: items ID name type
Imagine I have two tables: PETS SPECIES The SPECIES table has two columns: ID

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.