2016-12-05 18 views
-3

我有这个非常具体的问题,我甚至无法决定如何处理。所以我在MySQL中有3个表。特定的SQL问题:如何选择ALL ID匹配的行...(MySQL)

recipeid_recipe | name | text | picture

ingredients_recipeid_rs | id_recipe | id_ingredients

ingredientsid_ingredient | name | picutre

这是一个网站,在那里你选择的成分(使输入为1以上id_ingredient),它应该显示三类:现在

  1. 所有的食谱,你可以让(你把所有的成分需要它)
  2. 所有食谱,你只是缺少1或2成分
  3. 所有食谱,你只缺少3或4成分。

你能帮我这3个SQL选择吗?我现在非常僵硬。谢谢。

样本数据http://pastebin.com/aTC5kQJi

+4

你一定尝试过一些东西吧? –

+0

示例数据和预期输出将帮助您获得更准确的答案。 – Susang

+2

添加一些示例表格数据和预期结果(以及格式化文本)。同时向我们展示您当前的查询尝试。 – jarlh

回答

0

我觉得你的基本说法是已经在正确的轨道上。你只需要做一点小动作。您不能直接对它们进行比较,但是可以比较成分的计数:

SELECT id_receipe, count(id_rs) as ingredient_count 
    FROM ingredients_recipe 
    WHERE id_ingredient IN (2, 5) 
    GROUP BY id_recipe 

这会给你你必须为每个receipe成分的数量。现在得到每个食谱的成分总量

SELECT id_receipe, count(id_rs) as ingredient_count 
    FROM ingredients_recipe 
    GROUP BY id_recipe 

一个比较他们。以第一个查询为基础。你可以很容易地得到你的三个不同的类别。

+0

谢谢,完美的解决方案。非常简单!我需要检查我的SQL。 –