2016-09-15 6 views
1
<?php 
error_reporting(E_ALL); 
ini_set('display_errors' ,1); 
require "connection.php"; 


$query= "SELECT client_id, array_agg(insurance) AS insurance from vouchers WHERE parsing_date=CURRENT_DATE GROUP BY client_id "; 
$result = pg_query($conn,$query); 


?> 

<!DOCTYPE html> 
<html> 
<head> 
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet"> 
<link href = "http://fonts.googleapis.com/css?family=Roboto:400"> 

<style> 
.responstable { 
    margin: 1em 0; 
    width: 100%; 
    overflow: hidden; 
    background: #FFF; 
    color: #024457; 
    border-radius: 10px; 
    border: 1px solid #167F92; 
    word-wrap: break-word; 
} 

</style> 

</head> 

<body> 
<div class="container-fluid"> 
     <div> 

      <h1>Clients</h1> 


     </div> 

<table class="responstable" rules='all' style='border-collapse: collapse;'> 
<thead> 
    <tr> 
     <th>Client id</th> 
     <th>Insurance</th> 
     <th>Number of rows</th> 

    </tr> 
</thead> 
    <?php 
    while($row = pg_fetch_array($result)) 
    { 


    ?> 

    <tbody> 


    <td><?php echo $row['client_id']; ?></td> 
    <td><?php echo $row['insurance']; ?></td> 
    <td><?php echo $row['rows'];?></td>  
    </tr> 
    <?php } 


    ?> </tbody> 
</table> 

</div> 

</body> 
</html> 

전에서 내 출력을 위의 코드를 가지고 그것을 해결하는 방법을, 내가 추가를 시도그룹화 특정 행이 함께

Client id   Number of rows     Insurance 
------------  -----------------------  -------------- 
123       3 rows     AA,EE,U 
125       2 rows     AA,UE 
126       1 rows     CU 
124       1 rows     UE 

임 확실하지 이 줄을 내 검색어에 입력 :

concat(count(*), ' rows') AS rows 

ror, 행이 나오기 위해 내가 할 수있는 것에 대한 아이디어와 "{"와 ""가 간다?

답변

0
JSON 구문 분석과 쿼리 문제가 해결된다

<?php 
error_reporting (E_ALL); 
ini_set ('display_errors', 1); 
require "connection.php";   
$query = "SELECT client_id, 
     count(*) AS iCnt, 
     array_agg(insurance) AS insurance from vouchers 
     WHERE parsing_date=CURRENT_DATE GROUP BY client_id "; 
$result = pg_query ($conn, $query);  
?> 
<!DOCTYPE html> 
<html> 
<head> 
<link 
    href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" 
    rel="stylesheet"> 
<link href="http://fonts.googleapis.com/css?family=Roboto:400"> 
<style> 
.responstable { 
    margin: 1em 0; 
    width: 100%; 
    overflow: hidden; 
    background: #FFF; 
    color: #024457; 
    border-radius: 10px; 
    border: 1px solid #167F92; 
    word-wrap: break-word; 
} 
</style> 
</head> 
<body> 
    <div class="container-fluid"> 
     <div> 
      <h1>Clients</h1> 
     </div> 
     <table class="responstable" rules='all' style='border-collapse: collapse;'> 
      <thead> 
       <tr> 
        <th>Client id</th> 
        <th>Insurance</th> 
        <th>Number of rows</th> 

       </tr> 
      </thead> 
<tbody> 
<?php while ($row = pg_fetch_array ($result)) { ?> 
    <tr> 
     <td><?php echo $row['client_id']; ?></td> 
     <td><?php echo implode(',',json_decode($row['insurance'])); ?></td> 
     <td><?php 
      $sRows = ''; 
      if($row['iCnt'] > 0) { 
       $sRows = $row['iCnt']==1?' row':' rows'; 
      }     
      echo ''.$row['iCnt'].$sRows; 
     ?></td> 
    </tr> 
<?php } ?> 
</tbody> 
</table> 
</div> 
</body> 
</html> 
1

사용자는 explode 기능을 사용할 수 있습니다. 이 코드 당신이이 MySQL의 좋은 일이

SELECT client_id, concat(cast(count(*) AS char),' rows') 

같은 쿼리

<?php while ($row = pg_fetch_array ($result)) { ?> 
<tr> 
    <td><?php echo $row['client_id']; ?></td> 
    <td><?php echo implode(',',json_decode($row['insurance'])); ?></td> 
    <td><?php 
     $sRows = ''; 
     if($row['iCnt'] > 0) { 
      $sRows = $row['iCnt']==1?' row':' rows'; 
     }     
     echo ''.$row['iCnt'].$sRows; 
    ?></td> 
</tr> 
0

변경을 시도합니다. 당신은

SELECT client_id, 
    count(*) AS cnt, 
    ''||count(*)||' rows' AS concatString 

https://www.postgresql.org/docs/8.3/static/functions-string.html

당신은 쿼리

에 다음과 같이 다른 경우를 추가 할 수는 다음과 같이해야한다이다, PostgreSQL을 위해 연결

http://www.mysqltutorial.org/mysql-cast/

에 대한 string에 캐스팅 integer를 입력해야

SELECT client_id, 
    CASE WHEN count(*)>0 THEN 
     CASE WHEN count(*)==1 THEN ''||count(*)||' row' 
     ELSE ''||count(*)||' rows' END 
    ELSE '' 
    END AS concatenatedString 

내가 성능

Postgres nested if in case query

+0

나는 그것을 시도를, I 이 오류가 발생했습니다 : 경고 : pg_query() : 쿼리가 실패했습니다 : ERROR : concat (bigint, unknown) 함수가 존재하지 않습니다 LINE 1 : SELECT client_id, concat (count (*), 'rows') AS 행, array_agg ...^힌트 : 아니오 함수는 주어진 이름 및 인수 유형을 찾습니다. 명시 적 타입 캐스트를 추가해야 할 수도 있습니다. in index.php 8 행 –

+0

또한 볼 수 있듯이 PostgreSQL을 사용하여 일반 SQL –

+0

업데이트 된 대답을 확인 – Sundar

0

당신은 array_to_string 사용할 모르는 :

SELECT 
    client_id, 
    concat(count(*), ' rows'), 
    array_to_string(array_agg(insurance),',') AS insurance 
FROM vouchers 
WHERE parsing_date=CURRENT_DATE 
GROUP BY client_id 
관련 문제