【发布时间】:2018-06-21 07:22:10
【问题描述】:
在recipie_ingredient 表中,我有不同的成分。
create table recipe_ingredient (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
rel_recipe INT(6),
rel_ingredient INT(6)
);
INSERT INTO recipe_ingredient (rel_recipe, rel_ingredient) VALUES(1, 32);
INSERT INTO recipe_ingredient (rel_recipe, rel_ingredient) VALUES(1, 99);
INSERT INTO recipe_ingredient (rel_recipe, rel_ingredient) VALUES(1, 123);
INSERT INTO recipe_ingredient (rel_recipe, rel_ingredient) VALUES(1, 123);
INSERT INTO recipe_ingredient (rel_recipe, rel_ingredient) VALUES(1, 227);
INSERT INTO recipe_ingredient (rel_recipe, rel_ingredient) VALUES(1, 395);
INSERT INTO recipe_ingredient (rel_recipe, rel_ingredient) VALUES(1, 403);
INSERT INTO recipe_ingredient (rel_recipe, rel_ingredient) VALUES(1, 403);
根据我的成分,我想获得包含我的成分的食谱。我想在食谱中加入一些独特的成分。在DB Fiddle我创建了一个表并插入演示数据,我还添加了我当前无法正常工作的SQL。
SELECT
COUNT(distinct(ri.rel_ingredient)) as allIngredient,
sum((ri.rel_ingredient) in (123,403)) have
FROM recipe_ingredient ri
GROUP BY ri.rel_recipe;
最终结果应该是 allIngredient: 6 和 have: 2. (DB Fiddle link)
【问题讨论】: