Add up each item and sort by total in MySQL? - Posted (913 Views)
Junior Member
Lon2
Posts: 151
151
Newbie here. Hoping to get some help on this. I'm writing an MySQL database application that is basically a list of songs where users vote on them. I'm trying to figure out how to count the numbers in a particular table's column. For example, I have the following:

Database: SongDB
This Table: Ratings
This Table's Column: RateID (auto #), Rating (1-5), SongID (1-50)

I want to count or add up (SUM) each individual "Rating" and sort the list of songs on an ASP page in ascending (ASC) order based on its total rating.
Do I need to create a function to first add up the individual ratings, then select them in ascending order? If so, how would I write this code?
Here's what I have now that sorts the song list alphabetically by song title:
Code:
"SELECT * FROM Songs ORDER BY SongTitle ASC"
<
 Sort direction, for dates DESC means newest first  
 Page size 
Posted
Average Member
cripto9t
Posts: 881
881
sql Sum() function used with "Group by" should get you what you want. This will explain it better than I can. http://www.w3schools.com/sql/sql_groupby.asp



<
    _-/Cripto9t\-_
Posted
Junior Member
Lon2
Posts: 151
151
Thanks cripto, I'll take a look at that. [^]
<
 
You Must enter a message