Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.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_Select_Conditional - Fatal编程技术网

SQL条件选择语句

SQL条件选择语句,sql,select,conditional,Sql,Select,Conditional,我将如何着手做以下工作 给出表格、配方、配方、配方、配方说明。如果配方的SELECT语句返回结果,我如何执行SELECT 如果存在配方,则返回RecipeItems和RecipeInstructions。不确定这是否是您要查找的内容,但假设存在键关系,请选择以下形式的语句: SELECT ri.* FROM Recipe r JOIN RecipeItem ri ON ri.RecipeID = r.RecipeID WHERE r.Name = @myName -- or other crit

我将如何着手做以下工作

给出表格、配方、配方、配方、配方说明。如果配方的SELECT语句返回结果,我如何执行SELECT


如果存在配方,则返回RecipeItems和RecipeInstructions。

不确定这是否是您要查找的内容,但假设存在键关系,请选择以下形式的语句:

SELECT ri.* FROM Recipe r
JOIN RecipeItem ri ON ri.RecipeID = r.RecipeID
WHERE r.Name = @myName -- or other criteria

SELECT ris.* FROM Recipe r
JOIN RecipeInstructions ris ON ris.RecipeID = r.RecipeID
WHERE r.Name = @myName -- or other criteria

。。。仅当配方ID存在时,才会返回配方详细信息。这是使用SQL检索子项的标准方法。

您想要内部联接:

SELECT *
FROM Recipe
    INNER JOIN RecipeItem ON RecipeItem.RecipeID = Recipe.RecipeID
    INNER JOIN RecipeInstruction ON RecipeInstruction.RecipeID = Recipe.RecipeID
WHERE Recipe.[Name] = 'the recipe name'

这是假设
RecipeItem
RecipeInstruction
都有一个名为
RecipeID
的外键,该外键将其链接回主
Recipe
表。

这是一个开始,但您的问题并不清楚:

Select RI.*
   , RInst.*
From Recipe AS R
inner join RecipeItem AS RI
on R.PK = RI.FK
inner join RecipeInstruction AS RInst
on R.PK = RInst.FK

主键(PK)需要以某种方式匹配这些表之间的外键(FK)字段。我建议列出一些字段。

不太清楚您在这里要问什么。更多详细信息?如果配方不存在,SELECT将不返回任何行。这不是与“仅当配方的SELECT语句返回结果时”相同吗?如何列出一些字段以显示它们如何关联/联接?这是一种特殊类型的数据库吗?-1因为没有足够的信息,没有起始代码,没有列,没有字段等等。为什么选择两个单独的sql语句作为答案,而这两个语句都只返回子表条目,而不返回配方表中的详细信息?谢谢,先生,这正是我想要的。是的,Recipe是父实体,RecipeItem和RecipeInstruction包含Recipe的外键。