在我的网页上,有一个名为 $submission 的变量。我想从下面的查询中准确显示 11 行:$submission 等于 $row["title"] 的行,它上面的 5 行,它下面的 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>"; 最佳答案 如果多行共享相同的“点”值,这有点棘手:points | title | submissionid------ + ----- + ------------ ... 50 | 'foo' | ABCD01234 <-- If (50, 'foo') is the "midpoint" record, 50 | 'bar' | EF7654321 <-- does (50, 'bar') come before or after? ...在这种情况下,我们需要施加一个命令。为方便起见,我们将按“点”降序然后“标题”降序进行排序。假设您的“中点记录”的点值为“@points”,标题为“@title”,我们会说中点“之前”的记录是那些 (points, title) > (@points, @标题)。类似地,中点“之后”的那些记录有它们的 (points, title) 把它放在一起,我们有:-- First, initialize user variables with the points and-- title of our midpoint (including midpoint)--SELECT @title := title, @points := points FROM submission WHERE title = ? -- bind your $submission variable bere LIMIT 1;-- Now, select six records greater than or equal to our-- midpoint.-- SELECT title, points, submissionid FROM ( SELECT title, points, submissionid FROM submission WHERE (points, title) >= (@points, @title) ORDER BY points ASC, title ASC LIMIT 6) gte-- and UNION those records with five records less than-- our midpoint-- UNION SELECT title, points, submissionid FROM ( SELECT title, points, submissionid FROM submission WHERE (points, title) < (@points, @title) ORDER BY points DESC, title DESC LIMIT 5) lt-- Finally sort the result set--ORDER BY points DESC, title DESC关于php - 显示中间行包含变量的查询中的 11 个连续行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8439777/
10-13 02:46