有谁能告诉我如何从数据库中获取并显示最大值?我的数据库中有多个值,标题为“gmd”,但是如何只显示前3个最大值?在这个例子中我该怎么做:
$query = "SELECT gmd FROM account";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
}
答案 0 :(得分:5)
使用查询来排序和限制结果。
SELECT gmd
FROM account
ORDER BY gmd DESC
LIMIT 3
使用您的获取阵列显示所有结果。
$query = "SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo $row["gmd"];
}
mysql_free_result($result);
答案 1 :(得分:1)
对于包含字符串的字段:
SELECT gmd FROM account ORDER BY CHAR_LENGTH( gmd ) DESC LIMIT 3
对于包含数字的字段:
SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3
答案 2 :(得分:0)
$ query =“SELECT gmd FROM account ORDER BY gmd
DESC”;
答案 3 :(得分:-1)
$query = "SELECT gmd FROM account ORDER BY gmd DESC limit 3";