What I have is a mySQL query that will select some drinks and return basic information about the drink, as well as a list of the ingredients of that particular drink, as well as the rating for the particular drink. I have 3 tables drinks, drinks_ratings, drinks_ing
So, my issue is that say I want to get information about drinks that contain vodka, and are in a highball glass I would run the query below…
It works, its just my issue is that it doesnt return ALL the ingredients back. For Example, if I return “randomDrinkName1” and it happened to have vodka and soda in it….when i get the information back it leaves out the soda because i said WHERE ing = voda, so I understand why this is happeneing…but Is there some other type of WHERE clause I can do to check if it has “vodka” and return it along with all the other ingredient information that might also be there?
I know I could just do a query before this query that gets be back ids that have vodka in them from my drinks_ing table.
But this seems like it could be bad idea …like if there were 1000s of drinks with vodka in them just to do a query on a select with a 1000 OR statements.
I’m interested if there is a way i can easily do this all in one query. thanks!
select dIngs.id,
dIngs.name,
dIngs.descrip,
dIngs.type,
dIngs.ing,
AVG(b.rating) as arating,
COUNT(b.id) as tvotes
from (
select a.id,
a.name,
a.descrip,
a.type,
concat (
'[',
GROUP_CONCAT('{\"ing\":', c.ing, ',\"parts\":', c.parts, '}'),
']'
) ing
from drinks a
left join drinks_ing c on a.id = c.did
where c.ing = "vodka"
and a.type = "highball"
group by a.id
) dIngs
left join drinks_ratings b on dIngs.id = b.id
group by dIngs.id
order by arating desc,
tvotes desc LIMIT 0,
50;
edit:
to illustrate a result that I would want to get is like this:
[0]
descrip = "the description text will be here"
arating = 0
id = 4
ing = [ {"ing": "vodka", "parts": 4}, {"ing": "soda", "parts": 2}, {"ing": "sprite", "parts": 2} ]
name = "awesomeDrink"
type = "highball"
tvotes = 0
but what im actually getting back just includes the vodka ing because thats what i was checking for
[0]
descrip = "the description text will be here"
arating = 0
id = 4
ing = [ {"ing": "vodka", "parts": 4} ]
name = "awesomeDrink"
type = "highball"
tvotes = 0
To be clear, if i dont supply something like where ing = vodka, i get all the ingredients back just fine. thats not the issue….
I need it to just check if one of the potential ingredients happens to be vodka, then basically return all the ing data…and if vodka isn’t a potential ingredient, ignore that drink and NOT return it.
edit:
what my tables look like..
drinks_ing
---------------
did (which is the drink id its associated with)
id (the id of the ingredient aka "vodka")
parts
drinks
---------------
id
name
description
type
timestamp
drinks_ratings
-----------------
id
userid
rating
timestamp
Your best approach may be to use a self join. This is where you reference the same table twice, but by different names. Roughly this would look like:
Update: Making this better correspond to the tables in question. This is saying: given all of the vodka ingredients, find all of the ingredients that are found in the same drink as that vodka ingredient.