SELECT name, price, photo FROM drinks, drinks_photos WHERE drinks.id = drinks_id
yeilds 5 rows (5 arrays), photo
is the only unique field in a row. name, price
get repeated (here, fanta-
name, price
repeat 3 times.) How do i get rid of these duplicates?
Edit: I want name, price
and all photo
for each drink.
id name price
1. fanta 5
2. dew 4
id photo drinks_id
1. ./images/fanta-1.jpg 1
2. ./images/fanta-2.jpg 1
3. ./images/fanta-3.jpg 1
4. ./images/dew-1.jpg 2
5. ./images/dew-2.jpg 2
This question is related to
mysql
multiple-tables
You will have the duplicate values for name and price here. And ids are duplicate in the drinks_photos table.There is no way you can avoid them.Also what exactly you want the output ?
In order to get rid of duplicates, you can group by drinks.id
. But that way you'll get only one photo for each drinks.id
(which photo you'll get depends on database internal implementation).
Though it is not documented, in case of MySQL, you'll get the photo with lowest id
(in my experience I've never seen other behavior).
SELECT name, price, photo
FROM drinks, drinks_photos
WHERE drinks.id = drinks_id
GROUP BY drinks.id
Source: Stackoverflow.com