I have the two following database tables:
News Table:
newsID, topic, categoryID
Category Table:
categoryID, title, parentID
I want to get the newsID, the topic, the title of the main category and only IF parentID is NOT 0, the title of the parent category.
Currently im stuck like that:
SELECT news.newsID, news.topic, category.title
FROM news
LEFT JOIN category
ON news.categoryID = category.categoryID
WHERE news.newsID = 1
I’m still missing the parent title if available. What’s the most performanced way to get it (if available)?
Thanks
Assuming that you have a
categoryIDcolumn in thenewstable AND that thecategorytable is a self-referencing hierarchy, meaning that a child’sparentIDreferences thecategoryIDcolumn of the parent row in the same table: