Sql 如何进行只返回精确参数列表的查询

Sql 如何进行只返回精确参数列表的查询,sql,postgresql,Sql,Postgresql,我有一个包含成分信息的表,我只想在值与我收到的参数完全相同的情况下返回 表格结构为: Name | Recipe Ingredient X | Recipe1 Ingredient Y | Recipe1 Ingredient Z | Recipe2 如果用户通知成分X,则查询不应返回,但如果用户通知X和Y,则查询应返回Recipe1。 如果成分X和Z被告知,它也不应该返回 有人能帮你查询吗?你可以使用条件聚合: select recipe from t gro

我有一个包含成分信息的表,我只想在值与我收到的参数完全相同的情况下返回

表格结构为:

Name           | Recipe
Ingredient X   | Recipe1
Ingredient Y   | Recipe1
Ingredient Z   | Recipe2
如果用户通知成分X,则查询不应返回,但如果用户通知X和Y,则查询应返回Recipe1。 如果成分X和Z被告知,它也不应该返回


有人能帮你查询吗?

你可以使用条件聚合:

select recipe
from t
group by recipe
having sum(case when name in (<name list>) then 1 else 0 end) = count(*) and
       sum(case when name not in (<name list>) then 1 else 0 end) = 0;
选择配方
从t
按配方分组
有sum(当name in()和1 else 0 end时的大小写)=count(*)和
总和(当名称不在()时为1,否则为0结束)=0;
条件测试所有成分都在配方中,没有其他成分