Walhalla Walhalla - 1 year ago 77
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 ...


enter image description here


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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download