我从最近十个条目中选择了工作,但我不确定如何从这十个条目中获得最受欢迎的条目?另外,我如何计算最受欢迎的条目和数量?把它输出到一个百分比?
<?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']."] ";
}
任何人都知道我在这里做错了什么?请
答案 0 :(得分:2)
SELECT data
FROM (
SELECT data
FROM table_answers
ORDER BY id DESC
LIMIT 10
) t
ORDER BY popularity
答案 1 :(得分:2)
这应解决问题 -
SELECT tableorder.*
FROM (SELECT *
FROM table
ORDER BY id DESC
LIMIT 10) tableorder
ORDER BY tableorder.popularity DESC
LIMIT 1
内部查询将基于id进行排序并获得前10名。外部将根据流行度再次对10行进行排序,并返回最受欢迎的行。