我正在用php构建一个数据分析工具。我正在查询我们的生产服务器Postgres并显示数据。
我在一个循环中运行我的select查询(日期作为变量传递),它显示如下输出:
第1次迭代:
Country sum(yesterday)
India 4500
Southafrica 5000 第2次迭代:
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>";
}
}发布于 2011-10-20 23:19:49
这有点不清楚你想要做什么。所有这些标签,我们不知道你是要求用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)(我根据您的输出创建了表并插入了测试数据)
但是,由于这个模糊的问题,我不确定这是你想要的,所以这只是一次尝试。
https://stackoverflow.com/questions/7836052
复制相似问题