简体   繁体   English

使用 PHP 获取 MySQL 表中用户的总积分

[英]Get total points of user in MySQL Table with PHP

桌子

I have a MySQL table with the columns "user" and "warningpoints" for each warning as you can see in the table above.我有一个 MySQL 表,其中包含每个警告的“user”和“warningpoints”列,如上表所示。 How can I get the user which has the most warningpoints in total in PHP?如何获得 PHP 中警告点总数最多的用户?

You can use GROUP BY and ORDER BY and LIMIT :您可以使用GROUP BYORDER BYLIMIT

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;

You can simply add below statements in your php code.您可以简单地在 php 代码中添加以下语句。

$sql = "SELECT User, Max(warningpoints) AS MaxWarningpoints FROM MyGuests GROUP BY User";
$result = $conn->query($sql);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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