I have the tables: juices, juice_ingredients and ingredients.
The juices table has the attributes:
- name
- barcode
The juice_ingredients table has the attributes:
- juice_id
- ingredient_id
And the ingredients table has
- name
- optional (boolean)
For the sake of customer's logistics various juices may have the same barcode, but with different ingredients, some of which are optional I need select, by barcode, the single juice that has no contains optional ingredient among its ingredients.
I signed up four ingredients: water (optional: false), sugar (optional: true), pineapple pulp (optional: false) and mint (optional: true). And signed up four juices: one only with water and pineapple pulp, other with water, pineapple pulp and sugar, other with water, pineapple pulp and mint, and other with water, pineapple pulp, mint and sugar. All with the same barcode. I make a query to select only the juice make with non optional ingredients, in this case water and pineapple pulp.
SELECT *
FROM juices
INNER JOIN juice_ingredients ON (juice_ingredients.juice_id = juices.id)
INNER JOIN ingredients ON (juice_ingredients.ingredient_id = ingredients.id)
WHERE juices.barcode = '000000000001' AND ingredients.optional = false
But it was returning multiple rows. What should change this query to bring only one, or the juice containing no optional ingredients in the composition?
DISTINCTi.e likeSELECT DISTINCT .....juicesthat are not referenced byjuice_ingredients(you actually only want to see the ones with a given barcode and you expect to be just one of this kind. But am I right with my simplification? If so, post the complete definitions ofjuicesandjuice_ingredients.)