0
SELECT p.name, p.price, COALESCE(s.size, 'Medium')
FROM product p
LEFT JOIN sizes s ON p.id = s.product
WHERE p.id = 1

If a product is not mentioned in sizes table, it doesn't select that product?

  id      name      price
  1.     cap         50
  2.     glove       75


 id      product                   size
  1.     2                       'small'
  2.     2                       'large'  
NestedWeb
  • 1,657
  • 2
  • 15
  • 31

2 Answers2

0

what about this:

     SELECT p.name, p.price, IF( EXISTS(
         SELECT size
         FROM sizes s LEFT JOIN product p ON p.id = s.product
         WHERE p.id = 1
          ), s.size, 'Medium') as siz
    FROM product p
    LEFT JOIN sizes s ON p.id = s.product
    WHERE p.id = 1 
echo_Me
  • 37,078
  • 5
  • 58
  • 78
-1

Your query is fine except the WHERE condition

SELECT p.name, p.price, COALESCE(s.size, 'Medium')
FROM product p
LEFT JOIN sizes s ON p.id = s.product

Here is the code at SQL Fiddle Let me know if meant to achieve something else with the query.

Dipendu Paul
  • 2,685
  • 1
  • 23
  • 20
  • To whoever marked it down, I know the solution is same as @NestedWeb posted, I just wanted to add a SQL Fiddle and ask if he wants something else to be done. – Dipendu Paul Mar 16 '14 at 12:13