Let\'s say I have a Product
, Category
, and Product_To_Category
table. A Product can be in multiple categories.
Product
SELECT p.name, cat_food.name, cat_flowers.name
FROM
product p
left outer join Product_to_category pc_food
on p.id = pc_food.Prod_id
left outer join Category cat_food
on pc_food.Cat_id = cat_food.id
AND cat_food.name = 'Food'
left outer join Product_to_category pc_flowers
on p.id = pc_flowers.Prod_id
left outer join Category cat_flowers
on pc_flowers.Cat_id = cat_flowers.id
AND cat_flowers.Name = 'Flowers'
It only works if you know the number of possible categories, to put them into columns. That's how (standard) SQL works, the number of columns is not dynamic.