w3father
w3father

Reputation: 569

how to get category details while displaying subcategories?

I have category and subcategory tables.

My subcategory 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?

Upvotes: 2

Views: 493

Answers (4)

Wesley van Opdorp
Wesley van Opdorp

Reputation: 14941

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.

Upvotes: 0

Kræn Hansen
Kræn Hansen

Reputation: 1565

You need to lookup the SQL command JOIN: Read up on SQL joins here

Upvotes: 0

Johan
Johan

Reputation: 76547

SELECT subcat.name, subcat.cat_id, cat.name FROM subcat
INNER JOIN cat ON (subcat.cat_id = cat.id)

Upvotes: 1

Tudor Constantin
Tudor Constantin

Reputation: 26861

You can achieve that with an INNER JOIN:

SELECT sc.*, c.* FROM subcategory sc INNER JOIN category c ON sc.category_id = c.id

Upvotes: 2

Related Questions