Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

mySQL help

Thread Tools
 
Search this Thread
 
Old 31 January 2005, 10:00 PM
  #1  
chump
Scooby Regular
Thread Starter
 
chump's Avatar
 
Join Date: Feb 2004
Posts: 266
Likes: 0
Received 0 Likes on 0 Posts
Default mySQL help

hi,

i know a bit about mySQL as i use it in my PHP pages pretty effectively.

What i can't get my head around though is how to structure a database for average scores? Say if i want to run a 'Poll' that collates an average customer rating for a particular company (there will be multiple companies). Do i need to store the results for each company in a separate table? Then work out the averages on the fly by doing a simple average calculation and displaying the result on the page?

I'm not sure how you could keep a running total - as averages (as far as my maths tells me) are based on all the results added together and then divided by the total instances...

I'm really unsure how to structure this in SQL so that it is efficient....

This might be a bit confusing but I wasn't sure how to explain it...

Thanks
Old 01 February 2005, 08:55 AM
  #2  
SJ_Skyline
Scooby Senior
 
SJ_Skyline's Avatar
 
Join Date: Apr 2002
Location: Limbo
Posts: 21,922
Likes: 0
Received 1 Like on 1 Post
Default

you wouldn't store this information, you would run it on the fly. I am hopeing you have a structure something like:

tblCustomer:
intCustomerID
strCustomerName
.....

tblCompany:
intCompanyID
strCompanyName
.....

tblRating:
intCustomerID
intCompanyID
intRating

In order to get the average rating per company:
SELECT AVG(intRating), DISTINCT(intCompanyID) FROM tblRating
- or -
SELECT AVG(intRating), intCompanyID FROM tblRating GROUP BY intCompanyID

Note: SQL may differ slightly for mySQL


HTH
Old 01 February 2005, 09:11 AM
  #3  
TopBanana
Scooby Regular
 
TopBanana's Avatar
 
Join Date: Jan 2001
Posts: 9,781
Likes: 0
Received 0 Likes on 0 Posts
Default

That's how I'd do it too. You could denormalise it a bit if you never cared about individual ratings...

tblCompany:
intCustomerID
strCompanyName
intCumulativeRating
intRatingN

Then
SELECT intCompanyID, strCompanyName, intCumulativeRating / intRatingN FROM tblCompany
Old 01 February 2005, 09:43 AM
  #4  
chump
Scooby Regular
Thread Starter
 
chump's Avatar
 
Join Date: Feb 2004
Posts: 266
Likes: 0
Received 0 Likes on 0 Posts
Default

Thanks guys - most helpful
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
Wurzel
Computer & Technology Related
2
07 May 2007 05:45 PM
g3m xr
Southern (England)
6
13 September 2006 11:38 PM
GaryK
Computer & Technology Related
2
08 December 2004 11:04 AM
SiDHEaD
Computer & Technology Related
7
03 November 2003 10:17 AM



Quick Reply: mySQL help



All times are GMT +1. The time now is 08:30 AM.