我有以下简化表格:
CREATE TABLE recipe(id int, name varchar(25));
CREATE TABLE ingredient(name varchar(25));
CREATE TABLE uses_ingredient(recipe_id int, name varchar(25));
我想做一个查询,返回包含鸡肉和奶油的食谱的所有 id。
我努力了
SELECT recipe_id FROM uses_ingredient INNER JOIN
(SELECT * FROM ingredient WHERE name="Chicken" OR name="Cream")
USING (name) GROUP BY recipe_id
HAVING COUNT(recipe_id) >= (SELECT COUNT(*) FROM theme);
这给了我:“错误 1248(42000):每个派生表都必须有自己的别名” ,并且可能也是错误的。
接下来我尝试了
SELECT recipe_id FROM
(SELECT * FROM ingredient WHERE name="Chicken" OR name="Cream") AS t
INNER JOIN uses_ingredient USING (name)
GROUP BY recipe_id HAVING
COUNT(recipe_id)>= (SELECT COUNT(*) FROM t);
给出“错误1146(42S02):表'recipedb.t'不存在”
我想避免创建临时表,包括使用 ENGINE=MEMORY。