如何在PostgreSQL查询中转换mysql查询(group_concat)

时间:2019-05-18 07:53:31

标签: php postgresql

我想在PostgreSQL查询中转换这个波纹管两个MySQL查询(特别是第二个查询),因为我想在PostgreSQL数据库中使用这个波纹管查询,所以我该如何在PostgreSQL中编写这个波纹管查询,以便这个波纹管查询支持PostgreSQL数据库。

            <?php

            $data = [];
            $branchs = [];
           $query=mysqli_query($con,"SELECT DISTINCT branchcode FROM brackinfo2 WHERE event_id BETWEEN 71 AND 75");

           while($values=mysqli_fetch_array($query)){
            $branchs[] = $values['branchcode'];
           }

    $query=mysqli_query($con,"SELECT CONCAT(section,'.', sub_sec_id) AS si, GROUP_CONCAT (CONCAT(branchcode,':',question_point)) as brands, section, sub_sec_id, point as fullmark FROM brackinfo2 WHERE event_id BETWEEN 71 AND 75 GROUP BY branchcode,si");

            while($values=mysqli_fetch_array($query)){
                     $data[$values['si']]['fullmark'] = $values['fullmark'];
                     $data[$values['si']]['section'] = $values['section'];
                    $tmp = explode(",", $values['brands']);
                    foreach ($tmp as $key => $value) {
                        $tmp2 = explode(":", $value);
                        $data[$values['si']]['branchs'][$tmp2[0]] = $tmp2[1];
                    }
                }
                ?>
        <table>
           <thead>
            <tr>
            <th>SI</th>
            <th>Full Marks</th>
            <th>Section & Indicator Name</th>
                <?php 
                foreach ($branchs as $branch) {
                    echo "<th> Branch Name<br>($branch)</th>";
                }?>
            </tr>
            </thead>
            <tbody>
                <?php 
                foreach ($data as $si  => $info)
                {
                    echo "<tr>";
                    echo "<td>$si</td>";
                    echo "<td>{$info['fullmark']} </td>";
                    echo "<td>{$info['section']} </td>";
                    foreach( $branchs as $branch)
                    {
                        if (isset($info['branchs'][$branch])) {
                            echo "<td>{$info['branchs'][$branch]}</td>";
                        } else {
                            echo "<td></td>";
                        }

                    }
                    echo "</tr>";

                }

                ?>
            </tbody>
        </table>

1 个答案:

答案 0 :(得分:0)

您需要使用 PHP PDO 来以相同的查询支持MYSQL和POSTGRESQL(大多数情况下),或者使用pg_connect()pg_query()pg_fetch_array()专门用于PostgreSQL。有关PHP PDO查询示例的更多信息,请参见此处

MYSQL中的连接示例:

<?php
try {
  $dbuser = 'mysql';
  $dbpass = 'abc123';
  $dbhost = 'localhost';
  $dbname='mysql';
  $dbh = new PDO('mysql:host=$dbhost;dbname=$dbname', $dbuser, $dbpass);
}catch (PDOException $e){
  echo "Error!: " . $e->getMessage() . "<br/>";
  die();
}
?>

POSTGRESQL中的连接示例:

<?php
try{
  $dbuser = 'postgres';
  $dbpass = 'abc123';
  $dbhost = 'localhost';
  $dbname='postgres';
  $dbh = new PDO("pgsql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
 }catch (PDOException $e){
  echo "Error : " . $e->getMessage() . "<br/>";
  die();
 }
?>