我试图将以下查询结果限制为仅排在前10位的回复。到目前为止,查询完全符合我的要求 - 即它按顺序提供顶级系统列表,按降序排列。我得到了所有31个回复,但我只想要前10个回复。我尝试过创建一个While Do / End While循环,但我无法确切地知道将它放在以下代码中的位置:
$sql = "
SELECT HelpDesk
, COUNT(*) as NumSys
FROM `systems_17-18`
WHERE HelpDesk <>''
AND HelpDesk <> 'NONE'
GROUP BY HelpDesk
ORDER
by NumSys DESC
";
$result = $conn->query($sql);
$mycount = 0;
if ($result->num_rows > 0) {
// output data of each row
echo "<table class='uk-table uk-table-condensed uk-table-hover uk-table-striped'>";
while($row = $result->fetch_assoc())
{
$mycount ++;
echo "<tr>" ;
echo "<td>";
echo "<a href=" . $row['webaddress'] . " >";
echo $row['SchoolDistrict'] . "</a>";
echo "</td><td># " . $mycount . " " . $row['HelpDesk'] . " used by " . $row[NumSys] . " district(s)";
echo "</td></tr>";
}
echo "</table>";
}
(注意:上面的代码正确地生成了下面的数据。但是,如何将列表限制为仅排在前10位的响应?使用While EndWhile循环?)
加州的学区使用各种帮助台系统:
1 MyTechDesk used by 52 district(s)
2 SchoolDude used by 40 district(s)
3 WebHelpDesk used by 29 district(s)
4 ZenDesk used by 15 district(s)
5 SpiceWorks used by 14 district(s)
6 KACE1000/2000 used by 13 district(s)
7 FreshDesk used by 8 district(s)
8 Track-IT! used by 6 district(s)
9 Custom used by 6 district(s)
10 HEAT (Help Desk) used by 6 district(s)
11 SysAid used by 5 district(s)
12 ServiceDeskPlus used by 4 district(s)
13 GroupLink HelpDesk used by 4 district(s)
14 Altiris used by 4 district(s)
15 GLPI used by 3 district(s)
16 HESK used by 3 district(s)
17 HelpDesk used by 2 district(s)
18 Cherwell used by 2 district(s)
19 OPRAS used by 2 district(s)
20 Manage Engine Service Desk used by 2 district(s)
21 Samanage used by 1 district(s)
22 Absolute used by 1 district(s)
23 OTRS used by 1 district(s)
24 SherpaDesk used by 1 district(s)
25 Applied HelpDesk used by 1 district(s)
26 Public School Works used by 1 district(s)
27 iTop used by 1 district(s)
28 Connectwise used by 1 district(s)
29 Asana used by 1 district(s)
30 RT/SRTS used by 1 district(s)
31 OSTicket used by 1 district(s)
答案 0 :(得分:1)
在LIMIT 10
声明之后添加ORDER BY
。
$sql = "SELECT HelpDesk, COUNT(*) as NumSys FROM `schoolt7_systems`.`systems_17-18` WHERE HelpDesk <>'' AND HelpDesk <> 'NONE' GROUP BY HelpDesk ORDER by NumSys DESC LIMIT 10";
编辑:从MS SQL Server更改为MySQL
答案 1 :(得分:0)
使用LIMIT
:
SELECT
HelpDesk,
COUNT(*) AS NumSys
FROM `schoolt7_systems`.`systems_17-18`
WHERE
HelpDesk <>'' AND
HelpDesk <> 'NONE'
GROUP BY
HelpDesk
ORDER by NumSys DESC
LIMIT 10 -- add this to your query