2016-09-15 9 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 

ロー、私は行が出てくるために何ができるかについてのアイデアと "{"と ""は行く?

答えて

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の機能を使用できます。 この

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

これは、MySQLに良い作品のようなクエリを変更

<?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

を解決している、私はそれを試してみました、私はこのエラーが発生しました:警告:pg_query():クエリが失敗しました:ERROR:concat(bigint、unknown)関数が存在しませんLINE 1:SELECT client_id、concat(count(*)、 'rows')AS行、array_agg ... ^ヒント:いいえ関数は指定された名前と引数の型と一致します。明示的な型キャストを追加する必要があるかもしれません。 in index.php in line 8 –

+0

また、PostgreSQLを使用しているImが通常の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 
関連する問題