ninjapew3 ninjapew3 - 6 months ago 25
MySQL Question

Get total points of user in MySQL Table with PHP

Table

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

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
LIMIT 1;

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
LIMIT 1;
Comments