Imagine you have an online store. The depth of subcategories isn't limited, so you may get some items pretty close to the top and some all the way at the bottom. You want to display breadcrumbs, something like:
Root->Category1->SubCategory2->SubCategory3->Item256
Root->Category4->SubCategory5->SubCategory6->SubCategory7->SubCategory8 ->Item789
You have a table that stores your items, something like:
tblItems(
itemID longint,
itemName varchar(100),
parentCategory longint)
You have table for categories:
tblCategories(
catID longint,
catName varchar(100),
catParent longint)
If catParent = 0 it's a root category.
Question is - what's the most efficient way to build a SELECT query (or stored procedure, or - whatever you think is good) to get a full breadcrumb path out of database.
The only restriction - it must be solved on the side of the database, not in some code.
Any ideas? My dev teammates already puzzled, so I am bringing this here









Bookmarks