Postgresql 如何获取选定列的数据?

Postgresql 如何获取选定列的数据?,postgresql,Postgresql,我有一张像下面这样的桌子 table1 --------- x y z total average ======================== 1 2 3 2 3 4 3 4 5 x y total avg 1 2 3 1.5 2 3 5 2.5 3 4 7 3.5 在这里,我需要什么是以往任何时候都选择在前端相应的列只列 需要一个总数和平均数 例如,如果我在前端选择x、

我有一张像下面这样的桌子

table1
---------    
x   y  z   total  average
========================
1   2  3 
2   3  4 
3   4  5 
x    y   total  avg
1    2    3     1.5
2    3    5     2.5
3    4    7     3.5
在这里,我需要什么是以往任何时候都选择在前端相应的列只列 需要一个总数和平均数 例如,如果我在前端选择x、y、total和average列 我需要像下面这样的输出

table1
---------    
x   y  z   total  average
========================
1   2  3 
2   3  4 
3   4  5 
x    y   total  avg
1    2    3     1.5
2    3    5     2.5
3    4    7     3.5
这里我将refcursor与dynamicsql一起使用

create or replace function sample_refcursor(i_column in varchar) returns refcursor as

$$
declare
c1 refcursor;
begin

drop   table if exists temp_t;
create  temp table temp_t as select x as A,y as B,z as C,total as tot,avg as average from table1;

open c1 for execute('select ' ||i_column|| ' from temp_t group by ' ||i_column);
return c1;
close c1;
end;
$$ language plpgsql

您可以尝试:
从表中选择x,y,x+y作为总计,(x+y)/2作为平均值

SELECT x, y, x+y AS tot, (x+y)/2 AS Avg From TableName 
I根据您的第二张表,您没有使用z

使用下面的代码

<?php
// Connecting, selecting database
$dbconn = pg_connect("host=localhost dbname=publishing user=www password=foo")
    or die('Could not connect: ' . pg_last_error());

// Performing SQL query
$query = 'SELECT x, y, x+y AS tot, (x+y)/2 AS Avg From TableName';
$result = pg_query($query) or die('Query failed: ' . pg_last_error());

// Printing results in HTML
echo "<table>\n";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "</table>\n";

// Free resultset
pg_free_result($result);

// Closing connection
pg_close($dbconn);
?>


如果我基于两列仅选择两列,则计算总计和average@MillaresRoo用户不需要查看,对不起。改变了。我怎么知道他们在前端应用程序中通过了多少列呢OK,我想我写的时候你已经改变了