JAN
JAN

Reputation: 21895

Join in Mysql doesn't produce the correct output

I have two tables :

Products table :

enter image description here

Family table :

enter image description here

I want to get all the family_name records of the products that have the same family_code as in the family table :

SELECT family_name 
FROM family
LEFT JOIN products
ON products.family_code=family.family_code;

But this code return all the family_name records .

Any idea where did I go wrong ?

Much appreciated

Upvotes: 1

Views: 31

Answers (1)

Jem
Jem

Reputation: 4583

If you do an INNER JOIN, it will only show you records which are in both tables. A LEFT JOIN will show all records from family regardless of whether they have a matching products table entry.

SELECT f.family_name 
FROM family f
INNER JOIN products p
ON f.family_code=p.family_code;

Upvotes: 2

Related Questions