ninjapew3 ninjapew3 - 1 year ago 176
MySQL Question

Get total points of user in MySQL Table with PHP


Hello! I have a MySQL table with the columns "user" and "warningpoints" for each warning as you can see in the table above. How can I get the user which has the most warningpoints in total in PHP?
Thanks in advance.

Answer Source

You can use GROUP BY and ORDER BY and LIMIT:

SELECT t.user,sum(t.warningPoints) as sum_points
FROM YourTable t
GROUP BY t.user
ORDER BY sum_points DESC

Or if there is only one record per person, no need to group :

SELECT t.user,t.warningPoints 
FROM YourTable t
ORDER BY t.warningPoints DESC