products(id, name)
categories(id, name, product_id)
I have a sample code:
products(1, 'Apple')
And categories
categories(1, 'Iphone', 1)
categories(2, 'Ipad', 1)
categories(3, 'Ipod', 1)
And query:
SELECT pr.id, cat.name, pr.name
FROM `products` as pr
LEFT JOIN `categories` as cat USING(id)
GROUP BY pr.id
But result is
1, "Iphone", "Apple"
And Ipad, Ipod not show, how to fix it?
1, "Iphone, Ipad, Ipod", "Apple"
Your JOIN condition is attempting to join the two ID columns, which is not the relationship. It should join the product.id to the category.produt_id
EDIT:
I realize that you want only one result with the categories concatenated. You need the GROUP_CONCAT function in your select list:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat