table :
========================================
|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 |
========================================
I wanna get maximum score in every document_id and then compare the result with other document_id. My goal is to get the sequence of 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'];
}
I still get a wrong result. Please, help me.
You can’t use alias in
ORDER BY. So you have to useMAX(score)inORDER BY. Like this:OR using sub-query you can do the same.
See this SQLFiddle