簡體   English   中英

MySQL通過臨時表循環

[英]Mysql looping through temporary table

我正在嘗試將查詢結果設置為變量,然后在另一個查詢中使用該結果。

所以現在我在成分表中有幾種類型的威士忌酒,我可以找到所有它們,並且具有:

CREATE TEMPORARY TABLE TempTable (Ingredient_Id int); 
Insert into TempTable Select Ingredient_Id from ingredients where INSTR(Ingredient_Name,'Whiskey')>0;

這給了我所有我需要的身份證。 然后我嘗試通過以下方式獲取相關數據:

select drink_names.*,ingredients.*, drink_recipes.*
from drink_recipes
Left JOIN drink_names ON drink_recipes.Drink_Id = drink_names.Drink_Id
Left JOIN ingredients ON ingredients.Ingredient_Id = drink_recipes.Ingredient_Id
where drink_recipes.Ingredient_Id in TempTable #This is the problem
Order By Drink_Name

我不知道如何使用TempTable每個ID運行此查詢

select drink_names.*,ingredients.*, drink_recipes.*
from drink_recipes
Left JOIN drink_names ON drink_recipes.Drink_Id = drink_names.Drink_Id
Left JOIN ingredients ON ingredients.Ingredient_Id = drink_recipes.Ingredient_Id
where drink_recipes.Ingredient_Id in 
(
  Select Ingredient_Id 
  from ingredients 
  where INSTR(Ingredient_Name,'Whiskey')>0
)
Order By Drink_Name

或者你也可以嘗試

select drink_names.*,ingredients.*, drink_recipes.*
from drink_recipes
Left JOIN drink_names ON drink_recipes.Drink_Id = drink_names.Drink_Id
Left JOIN ingredients ON ingredients.Ingredient_Id = drink_recipes.Ingredient_Id
                      AND INSTR(ingredients.Ingredient_Name,'Whiskey') > 0
Order By Drink_Name

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM