sql join two tables and calculate average

This are my tables table BUSINESS

BUSINESSUSERNAME|BUSINESSPASSWORD|BUSINESSNAME| Res1 |123 |Cafe | Res2 |456 |Foodtruck |

table USER

USERNAME|USERPASSWORD|NAME| user1 |123 |mr.1| user2 |234 |mr.2|

table FOOD

FOODID|FOODNAME|FOODPRICE|BUSINESSUSERNAME|
1     |CAKE    | 5       |Res1            |
2     |SHAKE   | 2       |Res2            |
3     |COLA    | 3       |Res1            |  

table FOOD_RATING

FOODREVIEWID|FOODID|FOORATING|BUSINESSUSERNAME|USERNAME|
1           |2     |3        |Res2            |user1   |
2           |2     |5        |Res2            |user2   |
3           |1     |4        |Res1            |user1   |
4           |3     |1        |Res1            |user1   |

i would like to get this table where BUSINESSUSERNAME = "Res1"

FOODNAME|FODPRICE|AVGRATING|
CAKE    |5       |4        |
COLA    |3       |1        |

Upvotes: 1

Views: 2094

Answers (1)

amdixon
amdixon

Reputation: 3833

query

select f.foodname, f.foodprice, avg(fr.foodrating)
from food_rating fr
inner join food f
on fr.foodid = f.foodid
group by f.foodname, f.foodprice
;

output

+----------+-----------+--------------------+
| FOODNAME | FOODPRICE | avg(fr.foodrating) |
+----------+-----------+--------------------+
| CAKE     |         5 |                  4 |
| SHAKE    |         2 |                  4 |
+----------+-----------+--------------------+

sqlfiddle

Upvotes: 1

Related Questions