I have a user table with id, username and food_id columns. The idea is the users store their favourite food and we come up with a league table of foods. I want to generate a report of the top votes for the each food type. I am using MySQL and PHP.
For clarity, here is an example of the table:
id food_id username
1 1 Bob
2 100 Jane
3 200 Andy
4 1 Maggy
5 100 Rich
6 100 Mick
7 1 Kevin
I have a query that user ‘renegm’ on Stackoverflow gave me. It give me the results of the food survey. The query is:
select food_id, count(*) score
from myTable
group by food_id
order by score desc limit 100
It gives me the results perfectly as in:
food_id score
1 3
100 4
I realised after I got the answer that I am getting food_ids and not names.
I need to do a join on food table. It looks like
food_id food_name
1 Salad
100 Burgers
How do I incorporate the join into the above query? I have looked at my books but can’t quite work it out.
1 Answer