Michoser
Michoser

Reputation: 85

Add table to query

I have query where I select id_categories and categories_names, now I added new table categories_description, same as categories_names but with descriptions (of course it has id_category too), how to modify this query to select k.id_category, kn.name and my new category_description from my new table categories_descriptions (sometimes there is no description for category).

SELECT
 c.id_category,
 cn.name,
FROM
 categories c,
 categories_names cn
WHERE
 c.id_category = cn.id_category

I am using PostgreSQL but I think in MySQL it will be the same.

Upvotes: 0

Views: 47

Answers (1)

Simo Kivistö
Simo Kivistö

Reputation: 4473

You mentioned that sometimes the description is missing so LEFT JOIN should be suitable:

SELECT c.id_category,
 cn.name,
 cd.category_description
FROM
 categories c
 JOIN categories_names cn ON c.id_category = cn.id_category
 LEFT JOIN categories_descriptions cd ON cn.id_category = cd.id_category

Upvotes: 1

Related Questions