我在下面有一个PostgreSQL表:
id | market | process | lines | items
----+----------------------------------------------------+------------+-------+-------
1 | Spain | Pick | 25 | 100
2 | Spain | Pick | 50 | 100
3 | UK | Pick | 100 | 100
4 | USA | Pick | 75 | 90
5 | Metz | Pick | 10 | 20
6 | UK | Pick | 76 | 90
7 | ROW | Pick | 200 | 300
8 | Russia | Pick | 150 | 1000
9 | South Africa | Pick | 35 | 50
10 | Uk | Pick | 15 | 20
11 | USA | Pick | 123 | 123
12 | Spain | Pack | 25 | 25
13 | Spain | Pack | 50 | 100
14 | UK | Pack | 100 | 100
15 | USA | Pack | 75 | 90
16 | Metz | Pack | 10 | 20
17 | UK | Pack | 76 | 90
18 | ROW | Pack | 200 | 300
19 | Russia | Pack | 150 | 1000
20 | South Africa | Pack | 35 | 50
21 | Uk | Pack | 15 | 20
22 | USA | Pack | 123 | 123
我需要的是在过程显示“ Pack”时获取lines列的总和,然后使用PHP在HTML页面上的表中显示总和。
我已经通过终端验证了SELECT SUM(lines) FROM jobs WHERE process='Pick';
代码,并且该代码有效,因此我将其放入了pg_query
中。
但是我无法在页面上显示总和。
我已经篡改了我已经获得的代码,该代码可以显示所有符合条件的数据,但是我无法使其正常工作。代码如下:
<?php
$db = pg_connect("host=localhost dbname=vcbv2 user=postgres");
$result2 = pg_query($db, "SELECT SUM(lines) FROM jobs WHERE process='Pick'");
echo "<table border='1'>";
echo "<tr><td>PICK LINES TO DO</td></tr>\n";
while ($row = pg_fetch_assoc($result2)) {
echo"<tr><td>{$row['$result2']}</tr>\n";
}
echo"</table>";
?>
答案 0 :(得分:2)
需要对您的代码进行一些小的更改
<?php
$db = pg_connect("host=localhost dbname=vcbv2 user=postgres");
$result2 = pg_query($db, "SELECT SUM(lines) as total FROM jobs WHERE process='Pick'");
echo "<table border='1'>";
echo "<tr><td>PICK LINES TO DO</td></tr>\n";
while ($row = pg_fetch_assoc($result2))
{
echo"<tr><td>{$row['total']}</tr>\n";
}
echo"</table>";
?>
这应该可以解决问题。在这里,您已经根据查询中的where条件添加了列。