MySQL中的有线子查询

时间:2019-01-01 05:01:04

标签: mysql

我有两个表RecipesIngredients,并且我有一个简单的子查询,如下:

SELECT
    RecipeTitle
FROM
    Recipes
WHERE
    Recipes.RecipeID IN (
        SELECT
            RecipeID
        FROM
            Ingredients

    )

我想知道为什么上面的sql没问题,因为Ingredients没有RecipeID列,所以当我运行以下查询时:

    SELECT
        RecipeID
    FROM
        Ingredients

发生错误,说Unknown column 'RecipeID' in 'field list'

在这里更新:

不会引起Unknown column 'RecipeID' in 'field list',如下所示:

SELECT
    RecipeTitle
FROM
    Recipes
WHERE
    Recipes.RecipeID/*1*/ IN (
        SELECT
            RecipeID /*no problem if this is the same as 1 in above line*/
        FROM
            Ingredients

    )

0 个答案:

没有答案