PostgreSQL 显示从查询中获取的数据

发布于 2024-12-11 06:46:10 字数 2141 浏览 0 评论 0原文

我正在用 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>";
    }
}

I am building a data analysis tool in php. I am querying our production server which is Postgres and displaying data.

I'm running my select query (date is passed as a variable) in a loop which display output like this:

1st Iteration :

Country     sum(yesterday)  
India       4500  
Southafrica 5000  

2nd Iteration :

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

I want to display it in a table like this .

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

I have written the DAL based on this tutorial

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

any help would be great .

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

Fetching Data :

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

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

Displaying data :

$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>";
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏舞 2024-12-18 06:46:10

有点不清楚你想做什么。所有这些标签,我们不知道您是要求在 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)

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

但是,由于问题含糊,我不确定这就是您想要的正在要求所以这只是一个镜头。

It's a bit unclear of what you are trying to do. All those tags and we do not know wether you're asking for table-builing the data in PHP, or doing it immediately in Postgres.

Assuming you mean in Postgres, here is one way to achieve what you want:

@> 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)

(I created the tables and inserted the test-data according to your output)

But again, due to the vague question I'm not sure this is what you're asking for so it's just a shot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文