如何在同一行的SQL中使用LIKE或CONTAINS子句?

时间:2017-05-23 15:39:21

标签: php sql

在我的项目中,您可以列出您拥有的成分,并搜索可以使用这些成分制作的食谱。这是我的示例SQL查询: 继承数据库https://i.imgur.com/LgZ0WMD.png 我想得到匹配列表。

org.apache.beam.sdk.ioPubsubIO

为什么此查询不起作用?

1 个答案:

答案 0 :(得分:2)

您可以使用in子句:

SELECT recipes_Name 
FROM tblrecipes
INNER JOIN tblingredients
ON tblrecipes.recipes_ID = tblingredients.ingredients_ID
WHERE ingredients_name in (variable1, variable2, variable3)

我的建议是通过安全使用prepared statements with PDO

<强>更新

我提出的查询只会获取指定所有成分的食谱(如果配方中缺少一种成分,则不会包含该食谱):

select
    rcp.*,
    ingr.selected_ingr_count
from
    tblrecipes rcp inner join(
        select
            ingredients_ID as recipe_id,
            count(*) as selected_ingr_count
        from
            tblingredients
        where
            ingredients_name in(
                'egg',
                'butter',
                'milk',
                'flour'
            )
        group by
            ingredients_ID
    ) as ingr on
    ingr.recipe_id = rcp.recipes_ID
    and ingr.selected_ingr_count =(
        select
            count(*)
        from
            tblingredients
        where
            tblingredients.ingredients_ID = rcp.recipes_ID
    )

这将导致:

recipes_ID      recipes_Name        selected_ingr_count
        1       cake                3
        3       brownies            2