我所拥有的是一个mySQL查询,它将选择一些饮料并返回有关饮料的基本信息,以及特定的饮料,以及特定饮品的评分。我有3个表饮料,drinks_ratings,drinks_ing如果这些行中至少有一行有给定的值,则选择具有给定ID的所有行
所以,我的问题是说,我想获得含有伏特加酒,并在一个高球杯中我会运行下面的查询饮料信息...
它的作品,它只是我的问题是,它不返回所有的成分。例如,如果我返回“randomDrinkName1”,它碰巧有伏特加和苏打水......当我得到信息时,它会留下苏打水,因为我说WHERE = VODO,所以我明白为什么会出现这种情况。 ..但是有没有其他类型的WHERE子句可以检查它是否含有“伏特加”,并将其与所有其他可能存在的成分信息一起返回?
我知道我可以在这个查询之前做一个查询,获取从我的drinks_ing表中获得的伏特加的ID。
但是,这似乎可能是一个坏主意......就好像有1000个饮料中含有伏特加,只是为了对带有1000个OR语句的选择进行查询。
我很感兴趣,如果有一种方法,我可以很容易地做到这一切在一个查询。谢谢!
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;
编辑: 说明,我会希望得到的结果是这样的:
[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
但什么我其实又回到刚才包括伏特加ING,因为这就是我被检查
[0]
descrip = "the description text will be here"
arating = 0
id = 4
ing = [ {"ing": "vodka", "parts": 4} ]
name = "awesomeDrink"
type = "highball"
tvotes = 0
需要明确的是,如果我不提供类似where ing = vodka
,我把所有材料都回来就好了。这不是问题....
我需要它来检查是否有一种潜在的成分恰好是伏特加,然后基本上返回所有的数据...如果伏特加不是一个潜在的成分,忽略喝,不返回它。
编辑: 什么我的表看起来像..
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
请发布一些示例数据来处理。 – vyegorov
为什么......为什么你在SQL中构建JSON,而不是用'json_encode()'在PHP中做这件事? – ThiefMaster
你可以发表你的表创建语句为3表和一些示例数据? – Ray