【问题标题】:PostgreSQL displaying data fetched from queryPostgreSQL 显示从查询中获取的数据
【发布时间】:2011-10-20 12:40:24
【问题描述】:

我正在用 php 构建一个数据分析工具。我正在查询我们的生产服务器,它是 Postgres 并显示数据。

我在一个循环中运行我的选择查询(日期作为变量传递),它显示如下输出:

第一次迭代:

Country     sum(yesterday)  
India       4500  
Southafrica 5000  

第二次迭代:

country     sum(day before)  
India       5000  
Southafrica 7000  
Japan       4000    

我想在这样的表格中显示它。

Country         yesterday   daybefore  
India           4500        5000  
Southafrica     5000        7000    
Japan           empty       4000  

我已经根据本教程编写了 DAL

http://net.tutsplus.com/tutorials/php/simple-php-class-based-querying/

任何帮助都会很棒。

Query sample : this is run twice in a loop where $date = array('1','2')                                                                                                                                              
query : select c.country_name, sum(tf.tx_amount_usd)
from
table1 tf,
table2 tp,
table3 d,
table4 c
where
tf.condition = tp.condition
and d.day = current_date-$date      
group by 1,2 order by 2

获取数据:

$results = array();
while ($row = pg_fetch_array($res)){
    $result = new DALQueryResult();
    foreach ($row as $k=>$v){
        $result->$k = $v;
    }

    $results[] = $result;
}
return $results;

显示数据:

$dal = new DAL(); 
$dates = array('1','2');                                                         
foreach ($dates as $date)  {
    $results = $dal->get_trans_by_date($date);
    echo "<h1>Data</h1>";
    // check if there were any results
    if ($results) {
        // cycle through results
        foreach ($results as $model) {
            /* echo "<pre>";print_r($results);echo "</pre>"; */
            echo "<li>$model->country_name ".number_format($model->sum,2)."</li>";
        }
    }
    else {
        // Display a message concerning lack of data
        echo "<p>No Query Output.</p>";
    }
}

【问题讨论】:

  • 如果您提供一些用于检索和输出数据的代码,将更容易给出答案。

标签: php html sql postgresql


【解决方案1】:

有点不清楚您要做什么。所有这些标签,我们不知道您是要求在 PHP 中对数据进行表构建,还是在 Postgres 中立即进行。

假设您的意思是在 Postgres 中,这是实现您想要的一种方法:

@> SELECT name AS country, yesterday_sum, daybefore_sum \
   FROM countries C \
   INNER JOIN iteration2 A ON C.id = A.country \
   LEFT JOIN iteration1 B ON C.id = B.country;

   country   | yesterday_sum | daybefore_sum
-------------+---------------+---------------
 India       |          4500 |          5000
 Southafrica |          5000 |          7000
 Japan       |               |          4000
(3 rows)

(我根据您的输出创建了表格并插入了测试数据)

但同样,由于这个模糊的问题,我不确定这是否是您要求的,所以这只是一个镜头。

【讨论】:

    猜你喜欢
    • 2022-07-26
    • 2018-07-09
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    相关资源
    最近更新 更多