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

  • SEARCH
  • Home
  • 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 8193935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:38:58+00:00 2026-06-07T04:38:58+00:00

I am trying to find the parent categories. Therefore i need to write, where

  • 0

I am trying to find the parent categories.

Therefore i need to write,

where CategoryID HAS a ParentCategoryID of 0

CategoryID could be 30 but if it has a ParentCategoryID of 0 then you know its the parent category.

This is my SQL so far:

SELECT        CategoryID, ParentCategoryID, Name, Published, Deleted, PictureID
FROM          Nop_Category
WHERE         (Deleted = 0) 
AND           (Published = 1) 
AND           (CategoryID = ParentCategoryID = 0)
  • 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-07T04:39:00+00:00Added an answer on June 7, 2026 at 4:39 am

    To perform equality checks against two fields, use the AND operator and specify the fieldname twice.

    SELECT        CategoryID, ParentCategoryID, Name, Published, Deleted, PictureID 
    FROM          Nop_Category 
    WHERE         (Deleted = 0)  
    AND           (Published = 1)  
    AND           (CategoryID = ParentCategoryID AND ParentCategoryID = 0) 
    

    But you could also write it like so and achieve the same results:

    SELECT        CategoryID, ParentCategoryID, Name, Published, Deleted, PictureID 
    FROM          Nop_Category 
    WHERE         (Deleted = 0)  
    AND           (Published = 1)  
    AND           (CategoryID = 0 AND ParentCategoryID = 0) 
    

    However, in your question, you mentioned that CategoryID could be 30, so your query won’t work. You’ll likely want to leave out the CategoryID or specify a specific categoryId through a parameter:

    SELECT        CategoryID, ParentCategoryID, Name, Published, Deleted, PictureID, 
    FROM          Nop_Category 
    WHERE         (Deleted = 0)  
    AND           (Published = 1)  
    AND           (CategoryID = @categoryID AND ParentCategoryID = 0) 
    

    EDIT:

    so if the categoryID is the same as the CetegoryParentID i know its a
    child.

    Generally when I do self-related tables, I use NULL for the ParentId which tells me that the current row is the parent. If you’re using 0 for null, then a record with a CategoryId of 30 and a ParentCategoryId of 30 means it’s neather a child nor a parent.

    ID  PID  Value
    0   0    Top Level - must exist for referential integrity
    1   0    Child of "Top Level"
    2   0    Another child of "Top Level"
    3   1    Child of "Child of Top Level"
    

    In this scenerio, you can only have 1 top level category, ALL others will be children (even though you consider a ParentCategoryId of 0 a parent, it still must live under CategoryId 0)

    Using NULL

    ID  PID  Value
    1   Null Top Level cat 1
    2   Null Top Level cat 2
    3   1    Child of "Top Level cat 1"
    4   2    Child of "Top Level cat 2"
    

    In this scenerio, I can easily find ALL top level categories

    SELECT * FROM dbo.Category WHERE pid IS NULL
    

    Or, if I want the top category for a specific category

    SELECT * FROM dbo.Category WHERE CategoryId = 1 AND ParentCategoryId is null
    

    And my referential integrity is completely in tact.

    To locate immediate children of a parent, simply pass in the categoryid of the parent you’re looking for:

    SELECT * FROM dbo.Category WHERE ParentCategoryId = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use Maven2 but my child projects cannot find the parent project.
any better way to write this ? $(this).parent().parent().find( dd ul).toggle(); update.. I am trying
I'm trying to find a way to change a selector's height to its parent's
I'm trying to find a way to make a list of parent object with
I'm trying to find a way to return the value of an array's parent
Ideally, im trying to click a div, find the next class within a parent.
I am trying to use jquery's parents/siblings to find particular input elements but I
I'm sure that this has been asked before, but I don't know what to
I'm trying to find the class .post-info inside the parent of a clicked element,
I'm having a problem with trying to find out whether the parent element does

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.