-1

桌子 :

========================================
|id| document_id | sentence_id | score |
========================================
|1 |   3         |     1       | 0.324 |
|2 |   3         |     2       | 0.201 |
|3 |   5         |     1       | 0.181 |
|4 |   5         |     2       | 0.402 |
========================================

我想在每个中获得最高分,document_id然后将结果与其他结果进行比较document_id。我的目标是获得序列document_id. so from the example, the result must be 5 3

$q = mysql_query("SELECT document_id, MAX(score) as max_score FROM `tb_score` GROUP BY document_id ");
while ($row = mysql_fetch_array($q)) {
   $min = $row['max_score'];
}

我仍然得到错误的结果。请帮我。

4

3 回答 3

1

您似乎描述的 SQL 是:

SELECT document_id
        , MAX(score) as max_score
    FROM tb_score
    GROUP BY document_id
    ORDER BY MAX(score) DESC --or max_score, I'm not sure which MySQL will accept.

这有帮助吗?

另外: MySQL扩展在 PHP 中已弃用,您应该使用MySQL 改进PHP 数据对象来完成这项工作。

于 2012-10-10T03:41:35.727 回答
1

您不能 ORDER BY. 所以你必须使用MAX(score)in ORDER BY。像这样:

SELECT document_id ,MAX(score) as Highscore
FROM tb_score
GROUP BY document_id
ORDER BY MAX(score) DESC;

或者使用子查询你可以做同样的事情。

SELECT * FROM
(
SELECT document_id ,MAX(score) as Highscore
FROM tb_score
GROUP BY document_id
) A
ORDER BY Highscore DESC;

看到这个 SQLFiddle

于 2012-10-10T04:16:50.817 回答
1

SQL必须是这样的。

SELECT document_id, MAX(score) as max_score 
FROM `tb_score` GROUP BY document_id
ORDER BY max_score DESC
于 2012-10-10T03:52:24.667 回答