简体   繁体   中英

Order by total COUNT relative to repeating columns MySQL

Firstly, apologies for the terrible title, I couldn't think of any other way to explain it.

I have a table that looks something like this :

ID  plays  track_id
1   23     1
2   8      2
3   100    1
4   4      2
5   10     3

I want to return the results so all plays for the same track_id are added together and then ordered by the track_id with the most plays first. So, in table form, the results from the above would look like this :

plays   track_id
123     1
12      2
10      3

But I'm totally lost on how to formulate this MySQL query in php.

Any help greatly appreciated.

SELECT SUM(plays) as plays, track_id
FROM Tracks
GROUP BY track_id
ORDER BY SUM(plays) DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM