2

我可以从最后十个条目中进行选择,但不确定如何从这十个条目中获得最受欢迎的?另外,我将如何计算最受欢迎的条目的数量并将其输出到百分比?

<?php 
    $sql = "SELECT data FROM table_answers ORDER BY id DESC LIMIT 10"; 
    $result = mysql_query ($sql, $db); 
    while ($row = mysql_fetch_array ($result)) 
    { 
         echo "[".$row['data']."]"; 
    } 
?>

而且我也尝试过 WHERE 值,但它没有返回任何结果。

$sql = "SELECT data FROM table_answers WHERE id IN (SELECT id FROM table_answers
         ORDER BY id DESC LIMIT 10) ORDER BY popularity DESC LIMIT 1"; 
$result = mysql_query ($sql, $db); 
    while ($row = mysql_fetch_array ($result)) 
    { 
        echo " [".$row['data']."] ";  
    }  

有人知道我在这里可能做错了什么吗?请

4

2 回答 2

2
SELECT data
FROM (
    SELECT data
    FROM table_answers
    ORDER BY id DESC
    LIMIT 10
) t
ORDER BY popularity
于 2012-05-20T15:35:11.400 回答
2

这应该可以解决问题 -

SELECT tableorder.* 
FROM (SELECT * 
      FROM table
      ORDER BY id DESC 
      LIMIT 10) tableorder 
ORDER BY tableorder.popularity DESC 
LIMIT 1

内部查询会根据 id 排序,得到前 10 名。外部查询会再次根据流行度对前 10 行进行排序,返回流行度最高的行。

于 2012-05-20T15:37:48.687 回答