SQLite join命令4个表

时间:2013-02-25 13:15:58

标签: sql database sqlite join

我在sql中很小,我想要一些帮助。 我有3-4个表,我想制作一个sqlite命令来返回一些行。表格如下。

表:餐厅

| Name | menuID | REFERENCES [Menu]([menuID])|

表:菜单

| MenuID | Name |

表:成分

| IngredientID | Name |

表:Food_Incl_ingredient

| FoodIncIngredientID | FoodID REFERENCES [Food][FoodID] | IngredientID REFERENCES [Ingredient]([IngredientID])|

表:食物

| FoodID | Name | Type | MenuID REFERENCES [Menu][MenuID]|

如果他们没有我想说的那种成分,我想查询一下我的餐馆名称,食物名称和食物类型。 谢谢。

餐厅:

| Name     | MenuId |
  Goodys        2
  Koumbaris     1

表格菜单:

| MenuID |     Name     |
    1      koumbarisMenu
    2      goodysMenu

餐食:

| FoodID |     Name      |  Type  | MenuID |
   29     Chef's Salad    Starter     2
   31     Mama's Salad    Starter     2
   30     Ceasar's Salad  Starter     2

Table Food_Incl_ingredient:

| FoodInclIngredientID | FoodID | IngredientID |
          1               29           1
          6               31           1
          8               30           1

表成分:

| IngredientID |  Name  |
    1         Cereals
    2         Shellfish

SQL查询:

SELECT Restaurant.Name, Food.Name, Food.Type
FROM Restaurant
INNER JOIN Menu ON Restaurant.menuID = Menu.MenuID  
INNER JOIN Food ON Menu.MenuID = Food.MenuID  
INNER JOIN Food_Incl_ingredient ON Food.FoodID = Food_Incl_ingredient.FoodID  
INNER JOIN Ingredient ON Food_Incl_ingredient.FoodInclIngredientID = Ingredient.IngredientID  
WHERE Ingredient.Name == 'Cereals'  

预期结果:

| Name |      Name      |  Type  |
 Goodys  Chef's Salad     Starter
 Goodys  Mama's Salad     Starter
 Goodys  Ceasar's Salad   Starter

我得到的唯一结果是第一行。

2 个答案:

答案 0 :(得分:1)

问题出在哪里?只需加入您需要的所有表格,然后说出WHERE Ingredient.Name != 'meat'

SELECT Restaurant.Name, Food.Name, Food.Type
FROM Restaurant
  JOIN Menu ON Restaurant.menuID = Menu.MenuID
  JOIN Food ON Menu.MenuID = Food.MenuID
  JOIN Food_Incl_ingredient ON Food.FoodID = Food_Incl_ingredient.FoodIncIngredientID
  JOIN Ingredient ON Food_Incl_ingredient.FoodIncIngredientID = Ingredient.Ingredient.ID
WHERE Ingredient.Name != 'meat'

我希望我理解你的帖子中的表名是什么,主键是什么;)

答案 1 :(得分:0)

...if they don't have the ingredient i will say

这是一个非常基本的结构,让您编写一个查询来说明餐厅是否需要为其菜单购买配料

您通常会构建一些东西,以便餐馆可以有多个菜单,例如早餐,午餐,晚餐,也许是餐饮或聚会。

注意LEFT JOIN和最后使用COALESCE。

Restaurant
id
restaurantname

Menu
id
menuname   e.g. Breakfast, Lunch, Dinner, Catering, Party, Special Cultural Holiday

Dish
id
dishname   

Ingredient
id
ingredientname

- 一道菜有一种或多种成分

Dish_Ingredients
dishid
ingredientid

- 菜单上有一个或多个菜肴

MenuDish
menuid
dishid

- 餐厅有一个或多个菜单

RestaurantMenu
restaurantid
menuid

- 餐厅保存成分清单

RestaurantIngredients
restaurantid
ingredientid
quantityOnHand  

- 当菜肴调用没有库存记录的成分时,左连接将导致返回NULL值;因此COALESCE()

    select restaurantname, menuname, dishname, ingredientname, quantityOnHand
    from restaurant R 
    inner join restaurantmenu RM on R.id = RM.restaurantid
    inner join menu M on RM.menuid = M.id
    inner join menudish MD on MD.menuid = M.id
    inner join dish D on MD.dishid = D.id
    inner join dishingredient DI on DI.dishid = D.id
    inner join ingredients I on DI.ingredientid  = I.id

    LEFT JOIN

    restaurantingredients RI  on RI.ingredientid = I.id and RI.restaurantid = R.id   
    where Coalesce(quantityOnHand, 'none') as CurrentInventory