Add up each item and sort by total in MySQL?

Snitz™ Forums 2000
https://forum.snitz.com/forumTopic/Posts/67916?pagenum=1
05 November 2025, 05:15

Topic


Lon2
Add up each item and sort by total in MySQL?
30 November 2008, 11:44


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"
<

 

Replies ...


cripto9t
01 December 2008, 07:24


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



<
Lon2
01 December 2008, 09:31


Thanks cripto, I'll take a look at that. [^]
<
© 2000-2021 Snitz™ Communications