如何修改下面的查询以在$submission
等于$row["title"]
的行之前显示数据库中的5行? (当行按points
降序排序时。)
$sqlStr = "SELECT title, points, submissionid
FROM submission
ORDER BY points DESC";
$result = mysql_query($sqlStr);
$arr = array();
$count=1;
echo "<table class=\"samplesrec\">";
while ($row = mysql_fetch_array($result)) {
echo '<tr >';
echo '<td>'.$count++.'.</td>';
echo '<td class="sitename1">'.$row["title"].'</td>';
echo '<td class="sitename2"><div class="pointlink2">'.number_format($row["points"]).'</div></td>';
echo '</tr>';
}
echo "</table>";
答案 0 :(得分:0)
当然可以。使用where子句将搜索限制为仅在您要通过值引用的元素之前的元素,然后反向排序它们并使用limit来获取前五个。
可能更容易获得结果,然后在php中执行此操作,而不是尝试在mysql中执行此操作。
答案 1 :(得分:0)
此查询应该可以解决问题:
SELECT title, points, submissionid
FROM submission s
,(SELECT s1.points
FROM submission s1
WHERE s1.title = $submission
LIMIT 1) p
WHERE points > p.points
ORDER BY points
LIMIT 5
如果您想要逆转订单:
SELECT * FROM (
SELECT title, points, submissionid
FROM submission s
,(SELECT s1.points
FROM submission s1
WHERE s1.title = $submission
LIMIT 1) p
WHERE points > p.points
ORDER BY points
LIMIT 5) x
ORDER BY points DESC