Walhalla Walhalla - 27 days ago 8
SQL Question

SQL Statement with Join - missing record ... (simple noob request)

this is a noob-question, I know.
I have 2 tables:

DB Design

The data looks like this ...

products_services:

enter image description here

product_service_categories:

enter image description here

My SQL-Statement looks like this:

SELECT product_service_id, product_service_name, product_service_category_name
FROM products_services
JOIN product_service_categories ON
products_services.product_service_category_id = product_service_categories.product_service_category_id
WHERE products_services.company_id = '7'
ORDER BY product_service_category_name, product_service_name


The result looks like this:

SQL result

I am missing the entry for "ID 2 - Ernährungsberatung". The entry is missing because its "product_service_category_id" is "0".

Please help me with the correct SQL-Statement to get this record too.

Thank you!

Answer

Replace JOIN with LEFT JOIN
JOIN returns only matching rows.
LEFT JOIN returns all rows from the left table with possible matches from the right table.

Comments