【问题标题】:MySQL a left join and a few inner joins in a same queryMySQL 一个左连接和几个内连接在同一个查询中
【发布时间】:2018-12-05 06:13:39
【问题描述】:

我有几张桌子。

  1. 类别
  2. 项目
  3. restaurant_items
  4. 餐厅
  5. 新赞助商
  6. 地区

如果我简要解释一下这些表格,

categories 是食品类别。例如:- 蛋糕

item 就像食物的子类别。例如:- 巧克力蛋糕

restaurant_items 是属于特定餐厅的 item 实例。例如:- ABC 餐厅的巧克力蛋糕

restaurants 顾名思义只是一家餐厅,regionsrestaurant 所在的区域。

最重要的一个新赞助是餐厅宣传其食品作为广告的地方。

runday    | category_id | restaurant_id

2018-12-5 | 23          | 45

这个new_sponsoreds 表表明,我们需要在restaurant_items 上运行广告,其中category_id 是23,餐厅的id 是45。

我的用例

从给定的region 中获取所有restaurant_items,然后过滤它们

restaurant_itemscategory 和最后 右外连接 得到 new_sponsoreds

这样食品应用程序用户就可以从region(纽约)获取restaurant_items,并通过category(蛋糕)对其进行过滤。

此查询用于获取restaurant_items

SELECT restaurant_items.* FROM test2.restaurant_items
inner join items on restaurant_items.item_id = items.Item_id
inner join categories on items.category_id = categories.id
inner join restaurants on restaurant_items.restaurant_id = restaurants.Restaurant_id
where restaurants.region_id = 7 and categories.id = 33

这很好用。但是当我尝试将这个结果与这样的new_sponsoreds 表结合起来时,

SELECT restaurant_items.*,new_sponsoreds.* FROM test2.restaurant_items
inner join items on restaurant_items.item_id = items.Item_id
inner join categories on items.category_id = categories.id
inner join restaurants on restaurant_items.restaurant_id = restaurants.Restaurant_id
left outer join new_sponsoreds on categories.id = new_sponsoreds.category_id
where restaurants.region_id = 7 and categories.id = 33
and new_sponsoreds.runday = current_date()

此查询返回空行集。 但我所期望的是,此查询将为我提供来自regioncategory_id 的所有restaurant_items 过滤器,如果餐厅在今天推广一个类别,这也将包括结果。

id |restaurant_id | Item_id | runday    | category_id | restaurant_id
1  |12            | 23      |2018-12-04 | 25          | 12
2  |12            | 45      |null       | null        | null
3  |15            | 23      |null       | null        | null

这是我的例外结果表,其中restaurant_idItem_id 来自restaurant_items,其他来自new_sponsoreds

这里的第一条记录匹配left outer join,因为restuarant_id 12 在2018-12-04 在那里推广category_id 25。

第二条记录即使属于同一家餐厅也不匹配,它的category_id不等于25。

第三条记录与第一条记录具有相同的category_id。但它与联接不匹配,因为它属于不同的餐厅。

这是我的预期结果。但是在我将左连接部分添加到我的查询后,我什么也没得到。为什么会发生这种情况?

【问题讨论】:

  • 使用LEFT JOIN 而不是LEFT OUTER JOIN
  • 将最后一行上移一个

标签: mysql left-join inner-join


【解决方案1】:
SELECT restaurant_items.*,new_sponsoreds.* FROM test2.restaurant_items
inner join items on restaurant_items.item_id = items.Item_id
inner join categories on items.category_id = categories.id
inner join restaurants on restaurant_items.restaurant_id = restaurants.Restaurant_id
left outer join new_sponsoreds on categories.id = new_sponsoreds.category_id
and new_sponsoreds.runday = current_date() and new_sponsoreds.restaurant_id = restaurants.Restaurant_id
where restaurants.region_id = 7 and categories.id = 33

正如评论所建议的,我将 lat 线向上移动一个并将这部分添加到左外连接

and new_sponsoreds.restaurant_id = restaurants.Restaurant_id

【讨论】:

    猜你喜欢
    • 2012-03-29
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 2021-01-30
    • 1970-01-01
    • 2016-08-31
    • 2016-01-04
    相关资源
    最近更新 更多