如何显示从嵌套查询派生的数据透视表?

时间:2017-09-13 06:50:54

标签: php mysql mysqli while-loop

我有以下查询:

首先,我需要获取所有用户角色的默认包ID和角色。然后,我需要获取相应包ID的运算符及其保证金率。

最后,我需要在数据透视表中的不同列中显示角色/包明智的保证金率。

这是我的代码:

<table>
    <tr>
        <th>Operator</th>
        <th>Retailer Margin</th>
        <th>Distributor Margin</th>
    </tr>

    <?php
        // Get the default Package IDs of all the roles
        $pdefault = 1;
        $stmt = $mysqli->prepare("SELECT role,package FROM packages WHERE pdefault = ?");
        $stmt->bind_param('i',$pdefault);
        $stmt->execute();    // Execute the prepared query.
        $stmt->store_result();
        $stmt->bind_result($role,$package);

        while ($stmt->fetch()) {

            //Output

            //+-------------+-----------------+
            //| Role        | Default Package |
            //+-------------+-----------------+
            //| Retailer    | 1               |
            //+-------------+-----------------+
            //| Distributor | 2               |
            //+-------------+-----------------+

            // Get the margin rates of all the operators of all the default packages
            $stmt = $mysqli->prepare("SELECT operator, margin FROM margins WHERE package = ?");
            $stmt->bind_param('i',$package);
            $stmt->execute();    // Execute the prepared query.
            $stmt->store_result();
            $stmt->bind_result($operator,$margin);
            while ($stmt->fetch()) {

            //Output

            //+----------+--------------+----------+
            //| Package  | Operator     | Margin   |
            //+----------+--------------+----------+
            //| 1        | Airtel       | 3.00%    |
            //+----------+--------------+----------+
            //| 1        | Idea         | 1.00%    |
            //+----------+--------------+----------+
            //| 1        | Vodafone     | 2.00%    |
            //+----------+--------------+----------+
            //| 2        | Airtel       | 4.00%    |
            //+----------+--------------+----------+
            //| 2        | Idea         | 2.00%    |
            //+----------+--------------+----------+
            //| 2        | Vodafone     | 3.00%    |
            //+----------+--------------+----------+ 


    ?>

    <tr>
        <td><?php echo $operator ?></td>
        <td><!-- Retailer (Package ID 1) Margin here --></td>
        <td><!-- Distributor (Package ID 2) Margin here --></td>
    </tr>
    <?php } 
    } ?>

</table>  

决赛桌应该是这样的:

    +------------+------------------+-------------------+
    | Operator   | Retailer Margin  | Distrbutor Margin |
    +------------+------------------+-------------------+
    | Airtel     | 3.00%            | 4.00%             |
    +------------+------------------+-------------------+
    | Idea       | 1.00%            | 2.00%             |
    +------------+------------------+-------------------+
    | Vodafone   | 2.00%            | 3.00%             |
    +------------+------------------+-------------------+  

我认为我正在以错误的方式进行上述查询。我无法找出能够制作所需表格的方式。

4 个答案:

答案 0 :(得分:0)

首先这样做:

SELECT p.package, p.role, m.operator, m.margin FROM packages p JOIN margins m ON m.package = p.package

现在您的结果中包含每个包的字段packageroleoperatormargin

现在只输出。

答案 1 :(得分:0)

不使用两个请求,而是使用一个join关闭的请求。

"SELECT p.role, p.package, m.operator, m.margin FROM packages p join margin m on p.package = m.package "

答案 2 :(得分:0)

您可以使用此查询获取数据来轻松表示

  SELECT group_concat(p.role order by m.package),p.package, m.operator, group_concat(m.margin order by m.package) FROM packages p left join margin m on p.package=m.package WHERE pdefault = 1 group by m.operator;

答案 3 :(得分:0)

您可以在一个查询中执行此操作,然后首先在关联的2D数组中收集数据。然后迭代该数组以生成最终表。您还可以动态创建边距列的标题。它看起来像这样(未经测试):

<?php
$pdefault = 1;
$stmt = $mysqli->prepare("
    SELECT     m.operator, p.role, m.margin
    FROM       packages p
    INNER JOIN margins ON m.package = p.package
    WHERE      p.pdefault = ?
    ORDER BY   m.operator, m.package
");
$stmt->bind_param('i', $pdefault);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($operator, $role, $margin);

while ($stmt->fetch()) {
    $roles[$role] = 1;
    $result[$operator][$role] = $margin;
}
?>
<table>
    <tr>
        <th>Operator</th>
<?php foreach($roles as $role => $ignore) { ?>
        <th><?=$role?> Margin</th>
<?php } ?>
    </tr>
<?php foreach($result as $operator => $margins) { ?>
    <tr>
        <td><?=$operator?></td>
<?php    foreach($roles as $role => $ignore) { ?>
        <td><?=$margins[$role]?></td>
<?php    } ?>
    </tr>
<?php } ?>