[英]MySQL many-to-many relationship: Help With Query
My current code is this: 我当前的代码是这样的:
$select = $this->select()
->from(array('r' => 'recipes'), $this->getAdapter()
->quoteInto("r.*, MATCH(title, directions) AGAINST(?) AS score", $searchText))
->where('MATCH(title, directions) AGAINST(?)', $searchText);
//SQL: SELECT r.*, MATCH(title, directions) AGAINST('chicken soup') AS `score` FROM `recipes` AS `r` WHERE (MATCH(title, directions) AGAINST('chicken soup'))
I want to add an additional WHERE clause for finding only recipes that contain a certain ingredient. 我想添加一个额外的WHERE子句,以仅查找包含某种成分的食谱。 The problem I am having comes from the fact that the ingredients table and my recipe table have a many-to-many relationship, with a connecting table ingredient_recipes that has the columns 'id', 'recipe_id', and 'ingredient_id'.
我遇到的问题来自以下事实:配料表和我的配方表之间存在多对多的关系,连接表的配料表中有“ id”,“ recipe_id”和“ ingredient_id”列。
How do I do this? 我该怎么做呢?
Here's the plain SQL to search recipes by ingredient. 这是按成分搜索配方的简单SQL。 You shold be able to extrapolate the specifics from there.
您可以从那里推断出具体细节。
SELECT r.*
FROM recipes r
LEFT OUTER JOIN recipe_ingredients ri ON r.recipe_id = re.recipe_id
LEFT OUTER JOIN ingredients i on ri.ingredient_id = i.ingredient_id
WHERE i.ingredient_title = "[Your Ingredient Variable]"
GROUP BY r.recipe_id;
For multiple Ingredients: 对于多种成分:
SELECT r.*
FROM recipes r
LEFT OUTER JOIN recipe_ingredients ri ON r.recipe_id = re.recipe_id
LEFT OUTER JOIN ingredients i on ri.ingredient_id = i.ingredient_id
WHERE i.ingredient_title = "[First Ingredient Var]"
OR i.ingredient_title = "[Second Ingredient Var]"
GROUP BY r.recipe_id;
Although if you're trying to find something that has both (not just one or the other), you may need to do two queries (or at least a sub-select), which can be a pretty slow query on a large database. 尽管如果您要查找同时具有这两者的东西(而不仅仅是一个或另一个),则可能需要执行两个查询(或至少是子选择),这在大型数据库上可能是非常慢的查询。
The best I can think of for a single query would be something like this: 对于单个查询,我能想到的最好的方法是这样的:
SELECT r.*
FROM recipes r
LEFT OUTER JOIN recipe_ingredients ri ON r.recipe_id = re.recipe_id
LEFT OUTER JOIN ingredients i1 on ri.ingredient_id = i1.ingredient_id
LEFT OUTER JOIN ingredients i2 on ri.ingredient_id = i2.ingredient_id
WHERE i1.ingredient_title = "[First Ingredient Var]"
AND i2.ingredient_title = "[Second Ingredient Var]"
GROUP BY r.recipe_id;
Where you're creating another join for each additional ingredient. 在您为每种其他成分创建另一个联接的位置。 This query would not return recipes that had some - but not all - the ingredients, only recipes that have ALL the ingredients you're looking for.
此查询将不会返回包含某些(但不是全部)成分的配方,仅返回具有您要查找的所有成分的配方。
I'm not very familiar with MySQL, but could something similar to this be done? 我对MySQL不太熟悉,但是可以做些类似的事情吗?
SELECT r.*
FROM recipes AS r
LEFT JOIN recipe_sites AS s ON r.site_id = s.id
WHERE '%salt%' IN (SELECT i.name
FROM ingredients AS i
LEFT JOIN recipe_ingredients AS ri ON i.id = ri.ingredient_id
LEFT JOIN recipes AS r ON ri.recipe_id = r.id)
GROUP BY r.id;
What would be the correct way to do this? 正确的方法是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.