Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 子查询筛选的行太多_Sql_Tsql - Fatal编程技术网

Sql 子查询筛选的行太多

Sql 子查询筛选的行太多,sql,tsql,Sql,Tsql,我的子查询过滤掉了太多的结果。它应该过滤掉子查询中3个食谱中使用的12种成分。总共有79个成分,因此查询应该返回67行。目前我的查询返回54 我不确定为什么,但是如果我把第二个WHERE条件改为OR而不是,我得到68行,这只比我预期的少了一行 我正在尝试创建的查询: (5) 查找爱尔兰炖菜中未使用的所有成分的ID和名称, Pollo Picoso或烤牛肉。(2列,67行) 数据库关系图: 我想你只需要- select distinct R.RecipeID, I.IngredientName

我的子查询过滤掉了太多的结果。它应该过滤掉子查询中3个食谱中使用的12种成分。总共有79个成分,因此查询应该返回67行。目前我的查询返回54

我不确定为什么,但是如果我把第二个WHERE条件改为OR而不是,我得到68行,这只比我预期的少了一行

我正在尝试创建的查询:

(5) 查找爱尔兰炖菜中未使用的所有成分的ID和名称, Pollo Picoso或烤牛肉。(2列,67行)

数据库关系图:


我想你只需要-

select distinct R.RecipeID, I.IngredientName
from Recipes R
inner join Recipe_Ingredients RI on R.RecipeID = RI.RecipeID
inner join Ingredients I on RI.IngredientID = I.IngredientID
where R.RecipeTitle not in ('Roast Beef',  'Irish Stew',  'Pollo Picoso');
如果这不符合您的要求,请告诉我。

此查询:

select distinct ri.IngredientID
from Recipe_Ingredients ri inner join Recipes r
on r.RecipeID = ri.RecipeID
where r.RecipeTitle in ('Roast Beef', 'Irish Stew', 'Pollo Picoso')
返回要排除的12种成分。
只有表
Recipe\u配料
Recipes
需要加入。
现在左键将
成分
连接到该查询并仅返回不匹配的行:

select i.IngredientID, i.IngredientName
from Ingredients i
left join (
  select ri.IngredientID
  from Recipe_Ingredients ri inner join Recipes r
  on r.RecipeID = ri.RecipeID
  where r.RecipeTitle in ('Roast Beef', 'Irish Stew', 'Pollo Picoso')
) t on t.IngredientID = i.IngredientID  
where t.IngredientID is null
使用
不在
中也可以得到相同的结果:

select IngredientID, IngredientName
from Ingredients 
where IngredientID not in (
  select ri.IngredientID
  from Recipe_Ingredients ri inner join Recipes r
  on r.RecipeID = ri.RecipeID
  where r.RecipeTitle in ('Roast Beef', 'Irish Stew', 'Pollo Picoso')
)
请参阅。

结果是67行配料。

我只喜欢在对加入的结果感兴趣时才加入。这里的情况并非如此。你只想看到符合某些条件的成分。这是
中的成分,其中…
。条件属于
WHERE
子句。因此,您得到的查询很容易理解,因此具有良好的可维护性

另一方面,
DISTINCT
通常是一个写得不好的查询的标志(只是连接所有东西,然后抓住一些方法来摆脱我们自己不必要地产生的东西)。这也可能非常昂贵,因为这会创建一个更大的中间结果,然后必须对其进行排序以找到创建的重复项

我会使用
不在
中获得所有不在中的配料,其配方在中为“烤牛肉”、“爱尔兰炖肉”和“Pollo Picoso”

select ingredientid, ingredientname
from ingredients 
where ingredientid not in 
(
  select ingredientid
  from recipe_ingredients
  where recipeid in
  (
    select recipeid
    from recipes 
    where recipetitle in ('Roast Beef', 'Irish Stew', 'Pollo Picoso')
  )
)
order by ingredientid;

首先使用
不存在
查找所有配料的ID和名称为什么选择
配方。如果您只需要配料,为什么选择RecipeID
?你会使用3个嵌套的插件吗?
select ingredientid, ingredientname
from ingredients 
where ingredientid not in 
(
  select ingredientid
  from recipe_ingredients
  where recipeid in
  (
    select recipeid
    from recipes 
    where recipetitle in ('Roast Beef', 'Irish Stew', 'Pollo Picoso')
  )
)
order by ingredientid;