简体   繁体   中英

MySQL a left join and a few inner joins in a same query

I have few tables namely.

  1. categories
  2. items
  3. restaurant_items
  4. restaurants
  5. new_sponsoreds
  6. regions

If I briefly explain these tables,

categories is food categories. eg:- Cake

item is like a subcategory for food. eg:- Chocolate Cake .

restaurant_items is an instance of item which belongs to a particular restaurant. eg:- Chocolate cake in ABC restaurant .

restaurants is as it names implies just a restaurant and regions is an area where the restaurant located at.

MOST IMPORTANT ONE new_sponsoreds is where restaurants promote their food items as advertisements.

runday    | category_id | restaurant_id

2018-12-5 | 23          | 45

This new_sponsoreds table indicates, We need to run advertisements on restaurant_items where category_id is 23 in restaurant's id is 45.

MY USE CASE

Get all the restaurant_items from a given region and then filter those

restaurant_items by a category and finally right outer join this results with new_sponsoreds .

So that a food app user can get restaurant_items from a region (NEW YORK) and filter it by a category (CAKES).

This query is for get that 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

This is works fine. But when I try to join this results with the new_sponsoreds table like this,

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()

This query returns empty set of rows. But what I expect was this query will gives me all the restaurant_items filter from a region and a category_id and if a restaurant promote a category in today, this also will include the results.

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

This is my excepted results table where restaurant_id and Item_id from restaurant_items and others from new_sponsoreds

In here first record match the left outer join because restuarant_id 12 is promoting there category_id 25 in 2018-12-04.

Second record doesn't matches even if it belongs to the same restaurant, it's category_id is not equals to the 25.

Third record has the same category_id as the first record. But it doesn't match the join because it belongs to a different restaurant.

This is my expected results. But I'm getting nothing after I added the left join part to my query. Why this could happen?

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

As the comment suggested I move the lat line up one and added this part to the left outer join

and new_sponsoreds.restaurant_id = restaurants.Restaurant_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM