我有这个来自 sql 查询的数组推送。
Array (
[0] => Array (
[TICK] => 1015
[KD_AIRL] => GA
[JUM] => 2834763198 )
[1] => Array (
[TICK] => 266
[KD_AIRL] => JT
[JUM] => 382940267 )
[2] => Array (
[TICK] => 153
[KD_AIRL] => SJY
[JUM] => 321328100 )
[3] => Array (
[TICK] => 672
[KD_AIRL] => GA
[JUM] => 1131468200.51 )
[4] => Array (
[TICK] => 239
[KD_AIRL] => JT
[JUM] => 229913500.25 )
[5] => Array (
[TICK] => 95
[KD_AIRL] => SJY
[JUM] => 102319151.00 )
)
从上面的数组中,我想按 [KD_AIRL] 分组如下:
Array (
[0] => Array (
[TICK] => 1687
[KD_AIRL] => GA
[JUM] => 3966231398 )
[1] => Array (
[TICK] => 505
[KD_AIRL] => JT
[JUM] => 612853767 )
[2] => Array (
[TICK] => 248
[KD_AIRL] => SJY
[JUM] => 423647251 )
)
然后按 [KD_AIRL] 分组后,我想通过 [JUM] 限制该数组 DESC。所以数组应该是这样的:(例如,我想要限制 2 行)
Array (
[0] => Array (
[TICK] => 1687
[KD_AIRL] => GA
[JUM] => 3966231398 )
[1] => Array (
[TICK] => 505
[KD_AIRL] => JT
[JUM] => 612853767 )
)
请任何人帮我解决这个问题.. 我应该在 php 代码中做什么?谢谢和最好的问候。
更新 这是我从 sql server 2000 中的所有分支数据库中选择的 php 代码。
<?php
...
...
include "../../../config/dbreportis.php";
// Query for select name branch (NM_BRANCH) and its name database (NM_DATA) in server
$cbranch = mssql_query("SELECT NM_DATA, NM_BRANCH FROM Branch ORDER BY KD_BRANCH ASC");
while ($allbranch = mssql_fetch_array($cbranch)){
// Connect to each branches database (NM_DATA)
include "../../../config/db.php";
mssql_select_db($allbranch[NM_DATA]);
// Query for selecting all customer and all airline
if (($customer == 'all_customer') AND ($airl == 'all_airline')){
$kueri = "
SELECT TOP $batas COUNT(d.TICKNO) AS TICK, d.KD_AIRL, SUM(d.FARE + d.TAXIW +
d.VATVAL + d.TRANSFEE - d.DISVAL) AS JUM
FROM DInvoiceT d INNER JOIN
HINVOICE h ON h.INVNO = d.INVNO AND h.SRK = d.SRK LEFT OUTER JOIN
Airline a ON a.KD_AIRL = d.KD_AIRL
WHERE (h.INVDATE >= '$from') AND (h.INVDATE <= '$to') AND (h.INOUT = '$inout')
AND (h.SRK = 'TX') AND (h.CPRINT = 'Y') AND (h.SVD = '')
GROUP BY d.KD_AIRL
ORDER BY JUM DESC";
}
// Query for selecting all customer but specific airline
elseif (($customer == 'all_customer') AND ($airl != 'all_airline')){
$kueri = "
SELECT TOP $batas COUNT(d.TICKNO) AS TICK, d.KD_AIRL, SUM(d.FARE + d.TAXIW +
d.VATVAL + d.TRANSFEE - d.DISVAL) AS JUM
FROM DInvoiceT d INNER JOIN
HINVOICE h ON h.INVNO = d.INVNO AND h.SRK = d.SRK LEFT OUTER JOIN
Airline a ON a.KD_AIRL = d.KD_AIRL
WHERE (h.INVDATE >= '$from') AND (h.INVDATE <= '$to') AND (h.INOUT = '$inout') AND
(h.SRK = 'TX') AND (d.KD_AIRL = '$airl') AND (h.CPRINT = 'Y') AND (h.SVD = '')
GROUP BY d.KD_AIRL
ORDER BY JUM DESC";
}
$ticket= 0;
$sumvalue = 0;
$data = mssql_query($kueri);
while ($r = mssql_fetch_assoc($data)){
$nm_airl = mssql_query("SELECT NM_AIRL FROM Airline WHERE KD_AIRL = '$r[KD_AIRL]'");
$do_airl = mssql_fetch_array($nm_airl);
if ($do_airl['NM_AIRL'] == ''){
$nama_airline = '';
}elseif ($do_airl['NM_AIRL'] != ''){
$nama_airline = $do_airl['NM_AIRL'];
}
$do_nama_airline = "$r[KD_AIRL]-$nama_airline";
echo "<tr>
<td align='left'>$no</td>
<td align='left'>$do_nama_airline</td>
<td align='right'>$r[TICK]</td>
<td align='right'>".number_format($r[JUM], 2 , '.' , ',' );
echo "</td>
</tr>";
$ticket += $r['TICK'];
$sumvalue += $r['JUM'];
$ticket_all += $r['TICK'];
$sumvalue_all += $r['JUM'];
$no++;
}
if ($sumvalue > 0){
echo "<tr>
<td colspan='2' align='right'>Total $airline1 :</td>
<td align='right'>".number_format($ticket, 2 , '.' , ',' );
echo "</td>
<td align='right'>".number_format($sumvalue, 2 , '.' , ',' );
echo "</td>
</tr>";
}
}
echo "<tr>
<td colspan='2' align='right'>Grand Total</td>
<td align='right'>".number_format($ticket_all, 2 , '.' , ',' );
echo "</td>
<td align='right'>".number_format($sumvalue_all, 2 , '.' , ',' );
echo "</td>
</tr>";
echo "</table>";
mssql_close();
?>
通过上面的代码,如果我有 20 个分支并且想要显示前 20 个结果,脚本将给出 20*20 = 400 行的结果。但我想要的是一份报告,它显示 20 行并且还按 [KD_AIRL] 分组并按 [JUM] 排序 DESC。
因此,我尝试将上面脚本中的查询结果推送到新数组,然后按 [KD_AIRL] 对其进行分组,并按 [JUM] 对 DESC 进行排序,最后将其限制为 20(例如)。
注意 当我使用下面的解决方案脚本时,并非所有 [KD_AIRL] 分组(eq [GA] 再次重复):此结果取自 sql server 中的所有分支数据库。
Array
(
[GA ] => Array
(
[TICK] => 1863
[KD_AIRL] => GA
[JUM] => 3906190760.99
)
[JT ] => Array
(
[TICK] => 787
[KD_AIRL] => JT
[JUM] => 723166135.39
)
[SJY] => Array
(
[TICK] => 138
[KD_AIRL] => SJY
[JUM] => 164732319
)
[GAC] => Array
(
[TICK] => 101
[KD_AIRL] => GAC
[JUM] => 145669200
)
[GA] => Array
(
[TICK] => 52
[KD_AIRL] => GA
[JUM] => 60380240
)
[KP ] => Array
(
[TICK] => 29
[KD_AIRL] => KP
[JUM] => 31069700
)
[JT] => Array
(
[TICK] => 41
[KD_AIRL] => JT
[JUM] => 31058000
)
[QG ] => Array
(
[TICK] => 46
[KD_AIRL] => QG
[JUM] => 30951350.57
)
[ID ] => Array
(
[TICK] => 36
[KD_AIRL] => ID
[JUM] => 22508000
)
[MZ ] => Array
(
[TICK] => 26
[KD_AIRL] => MZ
[JUM] => 22081400
)
[AK ] => Array
(
[TICK] => 23
[KD_AIRL] => AK
[JUM] => 21385468
)
[TGN] => Array
(
[TICK] => 26
[KD_AIRL] => TGN
[JUM] => 20125400
)
[QZ ] => Array
(
[TICK] => 17
[KD_AIRL] => QZ
[JUM] => 12856500
)
[QZ] => Array
(
[TICK] => 11
[KD_AIRL] => QZ
[JUM] => 8810000
)
[IW ] => Array
(
[TICK] => 4
[KD_AIRL] => IW
[JUM] => 6965000
)
[RI ] => Array
(
[TICK] => 6
[KD_AIRL] => RI
[JUM] => 6494800
)
[IL ] => Array
(
[TICK] => 7
[KD_AIRL] => IL
[JUM] => 5572000
)
[SI ] => Array
(
[TICK] => 6
[KD_AIRL] => SI
[JUM] => 5260800
)
[6N ] => Array
(
[TICK] => 6
[KD_AIRL] => 6N
[JUM] => 4187520
)
[MV ] => Array
(
[TICK] => 2
[KD_AIRL] => MV
[JUM] => 3225735
)
)
我需要的是所有 [KD_AIRL] 分组和排序的 DESC 并最终限制为 20 行(例如)。如果有一种方法可以通过 sql 查询而不是 php 代码来获取我想要的报告,那就太好了。请给我这个案例的解决方案。非常感谢。