+1 vote
in Other by
I have a database of 30000 products and all products has assigned different categories. There is a separate table for all categories & sub-categories are also stored into same table. The table structure is something like this

Category_mst

category_id_pk, category_name, parent_id

Product_mst

product_id_pk, product_name, category_id_fk (reference to Category_mst->category_id_pk)

Categories are stored upto any number of sub levels. So there can be categories like below

Clothes > Shirts > Kids > Red Color

Products are stored on any level of Category. I want to make a query or a php script that can find out all the Categories that have no Products under it(upto any sub level).

How can I do this ?

Thanks in advance.

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You are trying to store hierarchical data in a database.

The naive approach to this is a very poor one, because MySQL does not support recursive queries (unlike Oracle or SQL-server).

Change your database design.

See: The Nested Set Model hierarchical data

Implementing a hierarchical data structure in a database

Related questions

+1 vote
asked Feb 1, 2022 in Other by DavidAnderson
+1 vote
asked Jan 30, 2022 in Other by DavidAnderson
...