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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:42:56+00:00 2026-05-19T02:42:56+00:00

I have two tables: Product ———————————— id group_id name quick_select ———————————— 1 1 product1

  • 0

I have two tables:

Product
------------------------------------
id   group_id   name   quick_select
------------------------------------
1      1       product1      1
2      3       product2      0
3      5       product3      1

Product_group
-----------------------
id   name   parent_id
-----------------------
1   group1     0
2   group2     0
3   group3     1
4   group4     1
5   group5     3

I making a navigation system for quick select products. I show categories to user and user can navigate in them by clicking category button, then goes level down to subcategory and goes down so many levels that finally it can’t go deeper – then I show products. First I show root categories where there’s products under them and under those root categories subcategories, subsubcategories and so on.

In my query I want select all root categories (where parent_id=0), if there’s products in them and in their subcategories and subsubcategories and so on, where quick_select must be 1 in product table. And I don’t know the deepness of categories – how many levels there are.

Is it possible with one query? Or do I need to do two queries?

I made so far, with this query:

SELECT pg.id, pg.name, pg.parent_id AS parent_id
FROM product_group AS pg
LEFT JOIN product AS p ON pg.id = p.group_id
WHERE pg.parent_id = 0 AND p.id IS NOT NULL AND p.quick_select = 1
GROUP BY pg.id

But I don’t receive root categories which subcategory is empty, which subcategory is empty and under this is one more subcategory with products with quick_select=1.

Sorry for my bad english.

I want to receive all categories where products with quick_select=1 are, not products

-- Category
|     |
|   product
|
-- Category
      |
   Category
      |
   Category
      |
 multiple products 
  • 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-19T02:42:57+00:00Added an answer on May 19, 2026 at 2:42 am

    The bad news is that you can’t do this in SQLite, at least with this data structure, since SQLite doesn’t support recursive SQL or window functions.

    If select performance is important, you can try to organize the data like this:
    http://articles.sitepoint.com/article/hierarchical-data-database/2

    Another option is to add the root id to each row at input time.

    Basically, at some point you will have to use multiple selects and determine the root id at the application level.

    Update:
    Ok, this is very much pseudo-code, but it should get you there.

    You need a language that has some sort of hashmap or named array datatype.

    hashmap results, parent, nodes, nodes_new; # variables
    
    
    foreach  (res in sql_execute("SELECT id, parent_id FROM product_group;") ) {
        parent[res.id] = res.parent_id;
    }
    
    # get groups with products
    foreach  (res in sql_execute("SELECT pg.id FROM product_group AS pg INNER JOIN 
            product AS p ON pg.id = p.group_id 
            WHERE p.quick_select = 1 GROUP BY pg.id ") ) {
        nodes[res.id] = res.id;
    }
    
    while (length(nodes) > 0) {
        foreach (i in nodes) {
            if (i = 0) { results[i] = i; } # if its a root node, add to results
            else { nodes_new[parent[i]] = parent[i]; } # otherwise, add parent to the next round
        }
        nodes = nodes_new; # prepare for next round
    }
    
    print results;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two product tables Product1 and Product2. There is a one 2 one
I have two tables Product (id int, brandid int,Name nvarchar(1000)) 2+ million rows Brand
I have two tables: **Product** ID Name SKU **Brand** ID Name Product table has
I have two tables as follows PRODUCT table Id | Name | Price And
I have two tables: product_description( name , product_id ) product( quantity, stock_status_id, price, product_id
We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType
I have two tables: [ product_to_category --------------------- product_id category_id [ category --------------------- category_id parent_id
I have two tables : Product and ProductRateDetail. The parent table is Product. I
I have two tables Product having following structure: ProductID,ProductName, IsSaleTypeA, IsSaleTypeB, IsSaleTypeC 1, AAA,
I'm quite a begginer and I have two tables: product and product attributes. Here's

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.