我有一个函数可以在数据库中搜索最高“分数”。 Db 的结构如下:
----------------------------------------
| id | UrlId | Article | Score |
----------------------------------------
我能正确得到最高分,但是我不知道如何根据最高分返回完整的对象。 我不愿意遍历整个表并测试“分数”的值以查看哪个是最高的(尽管当我输入时我怀疑我正在这样做)因为数据库可能有 10000 条记录。 我确信这非常简单,但我有“愚蠢,我今天无法动脑”有人知道更优雅的解决方案吗?
我的最终结果必须是这样的: 如果有 4 个 UrlId;s 具有相同的最高分,则用户需要查看:
UrlId example1 20(分数)
UrlId example2 20(分数)
UrlId example3 20(分数)
UrlId example4 20(分数)
不会显示所有其他结果。
function gethappiestBlog() {
$happiestBlogs = /* This is the data that I loop through, this is correct */
$happinessArray = array();
foreach($happiestBlogs as $happiestBlog) {
$happinessArray[]= $happiestBlog->Score;
}
$maxHappy = max($happinessArray);
echo $maxHappy;
}
最佳答案
SELECT fieldlist
FROM `tableName`
WHERE `score` = (SELECT MAX(`score`) FROM `tableName`)
关于php - 你如何根据表列中的最大值从 Mysql 返回行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13972424/