Skip to content
Advertisement

MySQL Select Count of Duplicate Value In Relation Table

I have 3 tables: foods, order_detail, and orders

Here are the records for table foods:

Here are the records for table order_detail:

And here are the records for orders table:

I want to show count order detail grouped by food type. I expected this result:

Here is my approach, but it still doesn’t show the expected result.

The result is:

How can I get the result I want? Thanks in advance!

Advertisement

Answer

Aggregation can work here, but you need to join across all three tables:

Note that we do a left join across all three tables in order to not drop any food type which might happen to have zero orders.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement