how to get category details while displaying subcategories?
I have category and subcategory tables.
My subca开发者_JS百科tegory table contains cat_id
column.
Now I want to displaying all sub categories with their category id and category name in one html table.
How can I achieve it in a single query?
You can achieve that with an INNER JOIN
:
SELECT sc.*, c.* FROM subcategory sc INNER JOIN category c ON sc.category_id = c.id
SELECT subcat.name, subcat.cat_id, cat.name FROM subcat
INNER JOIN cat ON (subcat.cat_id = cat.id)
You need to lookup the SQL command JOIN: Read up on SQL joins here
First of all, please check out this article. This is what you want and what you need - even if it looks complicated at first glance.
Second, as an solution to your current question. You will need to join the data from the tables. (Read JOIN SYNTAX). Other answers already have the exact query.
精彩评论