将表数据显示为列而不是php中的行

时间:2013-01-28 09:26:54

标签: php sql postgresql

我对postgres数据库有多个查询,我希望将数据返回到该数据库。当前数据在两列中的多行上返回,这是错误的。我喜欢将数据显示在一行和几列中。查询是这样的:

<?php

$sql = "Select ceil(SUM (a.CALLDURATION::integer) / 60) AS minutes,  sum(a.alltaxcost::integer) AS revenue
FROM cdr_data a,COUNTRY_CODES b
WHERE  a.CALLSUBCLASS = '002'
     AND a.CALLCLASS = '008'
and a.zoneiddest::integer > 0
AND SUBSTR (a.CALLEDNUMBER, 1, 2) NOT IN
('77', '78', '75', '70', '71', '41', '31', '39', '76','79')

and not substr(a.zoneiddest,1,3) in ('254','255','211','257','250','256')
and trim(a.zoneiddest)  = trim(b.country_code)

union

select  ceil(sum(callduration::integer/60) )as    total_minutes,round(sum(alltaxcost::integer) ,2)as revenue
from cdr_data 
where callclass ='008' and callsubclass='001'
and callduration::integer >0
and  regexp_replace(callednumber,'^256','') ~ '^73'
and bundleunits = 'Money'
and regexp_replace(callednumber,'^256','') ~ '^73'

union

select ceil(sum(callduration::integer/60) )as  total_minutes,round(sum(alltaxcost::integer) ,2)as revenue
from cdr_data 
where callclass ='008' and callsubclass='001'
and callduration::integer >0
and  identifiant ~'^73'
and bundleunits = 'Money'
and zoneorange <> '-1'

union

SELECT sum(a.alltaxcost::integer) AS revenue, ceil(SUM (a.CALLDURATION::integer) / 60)  AS minutes
FROM cdr_data a,  COUNTRY_CODES b
WHERE  a.CALLSUBCLASS = '002'
     AND a.CALLCLASS = '008'
and a.zoneiddest::integer > 0
AND SUBSTR (a.CALLEDNUMBER, 1, 2) NOT IN
('77', '78', '75', '70', '71', '41', '31', '39', '76','79')

and  substr(a.zoneiddest,1,3) in ('254','255','211','257','250')
and trim(a.zoneiddest)  = trim(b.country_code)
";


$result = pg_query($dbh, $sql);
if (!$result) {
 die("Error in SQL query: " . pg_last_error());
 }


  while ($row = pg_fetch_array($result)) {
  echo "<tr style='background-color: #FFFFFF;font-size:15px' align='center'>";
     echo "<td>" . $row[0] . "</td>";
     echo "<td>" . $row[1] . "</td>";
     echo "<td>" . $row[2] . "</td>";
     echo "<td>" . $row[3] . "</td>";
     echo "<td>" . $row[4] . "</td>";
     echo "<td>" . $row[5] . "</td>";
     echo "<td>" . $row[6] . "</td>";
     echo "<td>" . $row[7] . "</td>";
     echo "<td>" . $row[8] . "</td>";
     echo "<td>" . $row[9] . "</td>";
     echo "<td>" . $row[10] . "</td>";
     echo "<td>" . $row[11] . "</td>";

 echo "</tr>";
    }
 // free memory
 pg_free_result($result);       

 // close connection
 pg_close($dbh);
 ?>

这显示我的数据:

International Minutes   International Revenue   Onnet Minutes   Onnet Revenue                        
      0                         0                                       
     1343                         578086                                        

这不是我想要的。我希望结果像这样

International Minutes   International Revenue   Onnet Minutes     Onnet Revenue                      
      0                         0              1343                  578086                                 

所以我希望我的结果可以在一行和10列中返回。我怎样才能做到这一点。

3 个答案:

答案 0 :(得分:1)

因此,不要使用UNION,而是将每列作为自己的select语句。请注意,这可能效率不高。另一种选择是插入另一个表或视图,然后查询它。

这只是一个例子,但是像这样:

$sql = "Select 
          (Select ceil(SUM (a.CALLDURATION::integer) / 60)       
          FROM cdr_data a,COUNTRY_CODES b
          WHERE  a.CALLSUBCLASS = '002'
          AND a.CALLCLASS = '008'
          and a.zoneiddest::integer > 0
          AND SUBSTR (a.CALLEDNUMBER, 1, 2) NOT IN
          ('77', '78', '75', '70', '71', '41', '31', '39', '76','79')
          and not substr(a.zoneiddest,1,3) in ('254','255','211','257','250','256')
          and trim(a.zoneiddest)  = trim(b.country_code) as International Minutes
         ,(Select ...
           from ...
           where ...) as International Revenue
         ,(Select ... 
           from ...
           where) as Onnet Minutes
         ,(Select ...
           from ...
           where ...) as Onnet Revenue
         FROM ...
         WHERE...
         ";

答案 1 :(得分:0)

我不确定,但你可以尝试一下。

 echo "<tr style='background-color: #FFFFFF;font-size:15px' align='center'>";

 while ($row = pg_fetch_array($result)) 
 {
    echo "<td>" . $row[0] . "</td>";
    echo "<td>" . $row[1] . "</td>";
    echo "<td>" . $row[2] . "</td>";
    echo "<td>" . $row[3] . "</td>";
    echo "<td>" . $row[4] . "</td>";
    echo "<td>" . $row[5] . "</td>";
    echo "<td>" . $row[6] . "</td>";
    echo "<td>" . $row[7] . "</td>";
    echo "<td>" . $row[8] . "</td>";
    echo "<td>" . $row[9] . "</td>";
    echo "<td>" . $row[10] . "</td>";
    echo "<td>" . $row[11] . "</td>";
}

echo "</tr>";

答案 2 :(得分:0)

有一个名为tablefunc的postgresql扩展,其中包含功能交叉表,正是您所需要的。请参阅文档页面:http://www.postgresql.org/docs/9.1/static/tablefunc.html;

您需要先安装此扩展程序:

CREATE EXTENSION tablefunc;