1

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

JAN
  • 21,236
  • 66
  • 181
  • 318

1 Answers1

2

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;
Jem
  • 4,313
  • 2
  • 18
  • 20
  • 1
    You needn't specify inner as the [default join type is an inner join](http://stackoverflow.com/questions/4418776/what-is-the-default-mysql-join-standalone-inner-or). – hd1 Mar 26 '14 at 17:42
  • A great observation! I generally specify it for readability, especially in larger queries or with multiple joins (where the keyword helps it stand out). – Jem Mar 26 '14 at 17:44