我正在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>";
}
}
答案 0 :(得分:0)
你想要做什么有点不清楚。所有这些标签,我们不知道您是否要求在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)
(我根据你的输出创建了表并插入了测试数据)
但同样,由于模糊的问题,我不确定这是你所要求的,所以这只是一个镜头。