2013-12-18 4 views
0

내가 가진 테이블 같은 난 단지 이 필요로하는 프론트 엔드 해당 열에서 열을 선택하고 무엇도 필요 여기 선택한 열의 데이터를 가져 오는 방법은 무엇입니까?

table1 
---------  
x y z total average 
======================== 
1 2 3 
2 3 4 
3 4 5 

아래 총 예를 들면 평균 난에 X, Y, 총, 평균 열을 선택하면 전단 제가

여기
x y total avg 
1 2 3  1.5 
2 3 5  2.5 
3 4 7  3.5 

이하 dynamicsql refcursor로 사용하고 같은 출력을 필요

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 
,

답변

1

당신은 시도 할 수 있습니다 : 당신의 두 번째 테이블에 따라 당신은 Z를 사용하지 않는 SELECT x,y, x+y AS total, (x+y)/2 AS average FROM Table;

+0

아래

사용 미안, 내가 볼 Z – user2793872

+0

@MillaresRoo 사용자를 계산 두 개의 열을 기반으로 두 개의 열을 선택합니다. 변경됨. –

+0

필요하지 않습니다 총 평균 – MillaresRoo

1
SELECT x, y, x+y AS tot, (x+y)/2 AS Avg From TableName 

. 코드

<?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); 
?> 
관련 문제