0

我一直在寻找如何做到这一点,但我没有发现任何有用的东西!

所以我有下表,其中包含以下列:

ID | USER | COMMENTS
---------------------
1  | John | 20
2  | Sara | 32
3  | Peter| 10

我想做的是选择评论最多的用户。我在用着:

<?php
$usermaxresult = mysql_query("SELECT MAX(comments) FROM users"); 
while ($usermaxrow = mysql_fetch_array($usermaxresult)) {
$max = "MAX(comments)";
echo "$usermaxrow[$max]";
}
?>

但这只会返回最大评论的数量,而不是拥有最大评论的用户。

---- 工作!感谢您的评论,代码(它是葡萄牙语,因为我是葡萄牙人)

$usermaxuploads = mysql_query("SELECT MAX(uploads) as max_count FROM login");
$usermaxuploadsrow = mysql_fetch_array($usermaxuploads);
$maxvar = $usermaxuploadsrow["max_count"];

$usermaxresult = mysql_query("SELECT * from login WHERE uploads = '$maxvar' ");
$usermaxrow = mysql_fetch_array($usermaxresult);
echo $usermaxrow['usuario'];
4

3 回答 3

1

它应该给你你需要的东西:

SELECT MAX(comments) as tot, user FROM users;

tot评论的数量和user相对用户在哪里。

于 2013-06-08T09:40:22.893 回答
0

I don't know if I understand the problem wrongly or just other answers were wrong. How is SELECT MAX(comments) as max_count, user FROM users returns the users with the most comment? It returns the number of the highest comment and the first user (which might not be the user with the most comment). Shouldn't the below query be the correct one?

SELECT user, comments FROM users ORDER BY comments DESC LIMIT 0, 1
于 2013-06-08T10:32:44.820 回答
0

Yoy 可以ALIAS在对您的函数的查询中使用 an,MAX()以便稍后使用给定名称调用它。

$usermaxresult = mysql_query("SELECT MAX(comments) as max_count, user FROM users"); 

现在您可以使用

echo $usermaxrow['max_count'];
echo $usermaxrow['user'];
于 2013-06-08T09:40:51.667 回答