/    Sign up×
Community /Pin to ProfileBookmark

select from category and any sub category

I have a simple catalogue running. Products are inside basic high level categories..  I select items from this based on this ID. 

For example: SELECT * FROM products WHERE cateogryId = ?

However, I’ve now been asked to add sub categories.

So, if I had Cars before, I now have Cars > Toyota > 4WD

Cars: 2
Toyota: 34 (Parent 2)
4WD: 23 (Parent 34)

Now when I select ID 2, I want to also select those within 34 & 23..

How can I do this?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@sibertJun 21.2023 — You can use many-to-many relations. Which means that you must have a "link table" in between.

The link table consists of (normally) three columns (link_id, cat_id, sub_id)
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 7.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ,
analytics: Fullres
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @Nanditha,
tipped: article
amount: 1000 SATS,
)...