开发者

how to get category details while displaying subcategories?

开发者 https://www.devze.com 2023-03-08 23:23 出处:网络
I have category and subcategory tables. My subca开发者_JS百科tegory table contains cat_id column.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号