php - MySQL 中使用 PHP 的多维数组和聚合函数?

标签 php arrays multidimensional-array mysqli aggregate

已更新

如何在名为 CUMULATIVE TOTAL 的 u2 旁边再显示 1 列
它应该根据辅导员显示学生总数,应付总额,应付总额和应付总额。

假设我有 c1,c2,c3,c4 作为辅导员,u1,u2 作为大学 假设 c1 在每所大学中有 5 名学生,在这种情况下,CUMULATIVE TOTAL 列应将学生总数列显示为 [c1][No of students]=10,[c1][Payable]=some value,[c1][Paid]=一些值,[c1][Balence]=一些值

enter image description here


请检查以下代码,让我知道是否有任何方法可以在 SUM 聚合函数或任何替代解决方案中编写选择查询,因为我希望 wll_invoice.total_payable 应该按 customer_id 分组。

        <?php
define('DB_MAIN', 'localhost|user|passowd|database');

class my_db{

    private static $databases;
    private $connection;

    public function __construct($connDetails){
        if(!is_object(self::$databases[$connDetails])){
            list($host, $user, $pass, $dbname) = explode('|', $connDetails);
            $dsn = "mysql:host=$host;dbname=$dbname";
            self::$databases[$connDetails] = new PDO($dsn, $user, $pass);
        }
        $this->connection = self::$databases[$connDetails];
    }

    public function fetchAll($sql){
        $args = func_get_args();
        array_shift($args);
        $statement = $this->connection->prepare($sql);
        $statement->execute($args);
         return $statement->fetchAll(PDO::FETCH_OBJ);
    }
}

$db = new my_db(DB_MAIN);
$universities = $db->fetchAll('SELECT distinct customer_university FROM wll_customer');
$counselors = $db->fetchAll('SELECT distinct customer_counselor FROM wll_customer');
$payments_ = $db->fetchAll('SELECT
    customer_counselor,
    customer_university,
    COUNT(DISTINCT customer_name) AS \'no of students\',
    SUM(DISTINCT wll_invoice.total_payable) AS payable,**//I want to make total_payable should GROUP BY customer_id** 
    SUM(wll_invoice.total_pay) AS paid,
    SUM(wll_invoice.due) AS balance
FROM
    wll_customer
        LEFT JOIN
    wll_invoice ON wll_invoice.customer_id = wll_customer.customer_id
GROUP BY customer_counselor,customer_university;');

$payments = [];
foreach ($payments_ as $payment)
$payments[$payment->customer_counselor][$payment->customer_university] = $payment;
?>

<table id="table_id" class='display table-bordered'>
    <thead>
    <tr>
        <td rowspan="2">Sl</td>
        <td rowspan="2" >counselor</td>
<?php
    foreach ($universities as $key => $university){ ?>

        <td colspan="4" ><?=$university->customer_university ?> </td>
    <?php } ?>
    </tr>
    <tr>
    <?php foreach ( $universities as $university){?>
        <td>no of students</td>
        <td>payable</td>
        <td>paid</td>
        <td>balance</td>
    <?php } ?>
    </tr>
    </thead>
    <tbody>
    <tr>
    <?php foreach ( $counselors as $counselor){?>
    <?php foreach ( $universities as $key => $university){
     $payment = $payments[$counselor->customer_counselor][$university->customer_university];
    ?>  <?php if(!$key){?>
         <td></td>
         <td><?=$counselor->customer_counselor?></td>
        <?php } ?>
        <td><?=(int)$payment->{'no of students'}?></td>
        <td><?=number_format($payment->payable,0,',','')?></td>
        <td><?=number_format($payment->paid,0,',','')?></td>
        <td><?=number_format($payment->balance,0,',','')?></td>
    <?php } ?>
    </tr>
    <?php } ?>
    </tbody>
</table>

最佳答案

您的 SQL 应该按 customer_university 和 customer_counselor 分组:

SELECT 
customer_counselor, 
customer_university, 
COUNT(customer_name) AS \'no of students\', 
SUM(wll_invoice.total_payable) AS payable, 
SUM(wll_invoice.total_pay) AS paid, 
SUM(wll_invoice.due) AS balance 
FROM wll_customer 
LEFT JOIN wll_invoice 
 ON wll_invoice.customer_id = wll_customer.customer_id 
GROUP BY customer_counselor, customer_university

关于php - MySQL 中使用 PHP 的多维数组和聚合函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38699752/

相关文章:

php - 在 PHP 中使用 DateTime 作为单行代码

javascript - 如何在每个循环中对 Json 对象进行分组

php - 如何在指定端口的命令行中启动 apache2?

php - mySQL自动增量问题: key 1的条目“4294967295”重复

java - 使用compareTo()对字符串进行排序

scala - 将笛卡尔坐标组分组到apache Spark中的单元格

php - 按文件名对 $_FILES 进行排序

java - 如何在输出中随机生成字母?

php - 仅当有人提交新消息时加载数据

c - 在 C 中通过数组导航的指针问题