用于运行分组和限制针对多个数据库的查询的脚本

时间:2013-10-07 08:52:02

标签: php sql grouping limit

如果我想在sql server 2000中显示来自一个数据库的数据,那么我就像这样编写php脚本:

<?php
// connect to db branch
include "connect_dbbranch.php"
//example query
$kueri = "SELECT TOP 20 COUNT(ID) AS ID, SUM(NILAI) AS NILAI FROM table WHERE tgl =   
'09/01/2013' " ORDER BY NILAI DESC
$do=mssql_query($kueri);
//looping
while($result=mssql_fetch_array($do)){
 echo "$result[ID]<br>";
 echo "$result[NILAI]<br>"; 
}
?>

如果上面的脚本运行,它将显示DESC NILAI的20个数据顺序。 现在我想显示DESC NILAI的20个数据顺序,就像上面的脚本一样,但是来自所有数据库分支。

这是我的逻辑和脚本(它不显示20个数据,但是113个):

<?php
// connect ip server
include "../../../config/dball.php";
// select database name
$branch = mssql_query("SELECT NM_DATA, NM_BRANCH FROM Branch ORDER BY KD_BRANCH ASC");
while ($allbranch = mssql_fetch_array($branch)){
  //connect for each database
  include "../../../config/db.php";
  mssql_select_db($allbranch[NM_DATA]);

  //example query for selecting data
  $kueri = "SELECT TOP 20 COUNT(ID) AS ID, SUM(NILAI) AS NILAI
  FROM table WHERE tgl = '09/01/2013' " ORDER BY NILAI DESC
  $do=mssql_query($kueri);
  //looping
  while($result=mssql_fetch_array($do)){
   echo "$result[ID]<br>";
   echo "$result[NILAI]<br>";
  }
} // END LOOPING ALL BRANCH
?>

如何修复此问题,以便从所有数据库分支中获取20个数据?

0 个答案:

没有答案