Jump to content

[SOLVED] How to work top averages out.


Recommended Posts

Hi

 

Struggling to get my head around this. Basically I have system where users can rate restaurants.  I have table containing restaurant info.  In another table I have user ratings.  What I am trying to do is calculate the average rating of each restaurant and then display the top 5 restaurants with the highest average. 

 

What do you recommend is the best way to do this?  Calculate the top five averages in user rating and then join it with the restaurant table?

 

Many thanks

 

Link to comment
https://forums.phpfreaks.com/topic/174706-solved-how-to-work-top-averages-out/
Share on other sites

Cheers

 

CREATE TABLE IF NOT EXISTS `resturants` (

  `resturant_id` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(250) NOT NULL,

  `info` varchar(500) NOT NULL,

  `status` int(11) NOT NULL DEFAULT '1',

  PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

CREATE TABLE IF NOT EXISTS `ratings` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `resturant_id` int(11) NOT NULL,

  `rating` int(11) NOT NULL,

  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

You spelled restaurants wrong.

 

SELECT re.name, AVG(ra.rating) AS rating FROM resturants re LEFT JOIN ratings ra ON re.resturant_id = ra.resturant_id ORDER BY rating DESC LIMIT 5;

 

I'm not sure if you can do an ORDER BY from an aggregate function.  If you can this should give you what you want.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.