2016-08-01 98 views
2

修訂
使用PHP的MySQL中的多維數組和聚合函數?

如何顯示1多個列旁邊U2叫學生總數累計總
它應該顯示總人數總支付報酬,並根據輔導員總所致。

考慮我有C1,C2,C3,C4作爲輔導員和U1,U2作爲大學 說C1有5個學生,在這種情況下,累計總數列各大學應該向學生展示列的總數[C1] [沒有學生]的= 10,[C1] [應付帳款] =一定的價值,[C1] [付費] =一定的價值,[C1] [Balence] =一些價值

enter image description here


請檢查下面的代碼,讓我知道是否有任何方法來編寫選擇查詢內部SUM聚合函數或任何備用解決方案,因爲我想wll_invoice.total_payable應gr oup by 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> 
+0

哪個驅動程序不工作?什麼是錯誤信息? – bwoebi

+0

這是好的如果你可以請幫助我與我的查詢 – Sha

+0

這不是我的問題的答案?? – bwoebi

回答

2

我希望這是你的代碼正在尋找後:

<?php 
define('DB_MAIN', 'localhost|user|password|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 order by customer_university'); 
/** 
* Adding Cummulative university 
*/ 
$cumulativeUniversity = new StdClass(); 
$cumulativeUniversity->customer_university = "CUMULATIVE TOTAL"; 
$universities[] = $cumulativeUniversity; 

$counselors = $db->fetchAll('SELECT distinct customer_counselor FROM wll_customer order by customer_counselor'); 
$payments_ = $db->fetchAll('(SELECT 
    customer_counselor, 
    customer_university, 
    COUNT(distinct wll_invoice.customer_id) 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 
    order by `customer_counselor`, `customer_name`) 
UNION 
    (SELECT 
    customer_counselor, 
    "CUMULATIVE TOTAL" as university, 
    COUNT(distinct wll_invoice.customer_id) 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 
    ORDER BY `customer_counselor`)'); 

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

<table id="table_id" class='display table-bordered' border="1"> 
    <thead> 
    <tr> 
     <td rowspan="2" >Counselor</td> 
    <?php 
    foreach ($universities as $key => $university): ?> 
     <td colspan="4" ><?=$university->customer_university ?> </td> 
    <?php endforeach ?> 
    </tr> 
    <tr> 
    <?php foreach ($universities as $university): ?> 
     <td>no of students</td> 
     <td>payable</td> 
     <td>paid</td> 
     <td>balance</td> 
    <?php endforeach ?> 
    </tr> 
    <?php foreach ($counselors as $counselor):?> 
     <tr> 
      <td> 
       <?php echo $counselor->customer_counselor;?> 
      </td> 
     <?php foreach ($universities as $key => $university): 
      $payment = isset($payments[$counselor->customer_counselor][$university->customer_university]) ? $payments[$counselor->customer_counselor][$university->customer_university] : null; 
      if($payment):?> 
       <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 else:?> 
       <td colspan="4"></td> 
      <?php endif?> 
     <?php endforeach; ?> 
     </tr> 
    <?php endforeach; ?> 
    </thead> 
</table> 

我用下面的查詢在哪裏我正在使用Union來附加顧問的整體數據以及您正在查找的內容。另外,如果您已經注意到了代碼,那麼我已經在大學列表中附加了一個累積的大學對象來處理它的相同循環。

(SELECT 
customer_counselor, 
customer_university, 
COUNT(DISTINCT wll_invoice.customer_id) 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 
ORDER BY `customer_counselor`, `customer_name`) 

UNION 

(SELECT 
customer_counselor, 
"CUMULATIVE TOTAL" AS university, 
COUNT(DISTINCT wll_invoice.customer_id) 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 
ORDER BY `customer_counselor`) 

嘗試使用此查詢不同的值,但你真的需要更新您的架構。這只是一個臨時解決方案:

(SELECT 
customer_counselor, 
customer_university, 
COUNT(DISTINCT wll_invoice.customer_id) AS 'no of students', 
SUM(wll_invoice.total_payable) AS payable, 
SUM(final_pay) AS paid, 
SUM(wll_invoice.total_payable - final_pay) AS balance 
FROM wll_customer 

LEFT JOIN (SELECT MAX(id) max_id, customer_id, SUM(total_pay) final_pay FROM `wll_invoice` 
GROUP BY customer_id, `total_payable`) AS wll_unique ON wll_unique.customer_id = wll_customer.`customer_id` 

LEFT JOIN wll_invoice 
ON wll_invoice.customer_id = wll_unique.customer_id AND `wll_invoice`.id = wll_unique.max_id 
GROUP BY customer_counselor, customer_university 
ORDER BY `customer_counselor`, `customer_name`) 
UNION 
(SELECT 
customer_counselor, 
"CUMULATIVE TOTAL" AS university, 
COUNT(DISTINCT wll_invoice.customer_id) AS 'no of students', 
SUM(wll_invoice.total_payable) AS payable, 
SUM(final_pay) AS paid, 
SUM(wll_invoice.total_payable - final_pay) AS balance 
FROM wll_customer 

LEFT JOIN (SELECT MAX(id) max_id, customer_id, SUM(total_pay) final_pay FROM `wll_invoice` 
GROUP BY customer_id, `total_payable`) AS wll_unique ON wll_unique.customer_id = wll_customer.`customer_id` 

LEFT JOIN wll_invoice 
ON wll_invoice.customer_id = wll_unique.customer_id AND `wll_invoice`.id = wll_unique.max_id 
GROUP BY customer_counselor 
ORDER BY `customer_counselor`) 
+0

爲你的答案投票,其實我在我的數據庫total_payable列中有一些值,我需要得到不同total_payable的總和,並且應該由customer_id組成。除了代碼工作正常以外,請將其添加到您的代碼中。所以,我可以獎勵我的賞金給你,我會標記你的答案是正確的。 – Sha

+0

當然,我會的。你能否給我發送來自表格的數據樣本,並在問題中更新它?如果我是正確的,你在wll_customer表中有相同的customer_id的多個輸入,因爲你得到的是重複的? –

+0

我的意思是表的一些示例模式,以便我可以相應地更新我的查詢。 –

4

你的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 
+0

我在GROUP BY customer_counselor,customer_counselor – Sha

+0

之後得到了同樣的答案不,您可能獲得了緩存響應或者更改了錯誤的腳本。 – symcbean

+0

是的,現在它的工作,我想得到所有的大學基於輔導員的最後所有的大學綜合我怎麼做,你可以分享代碼 – Sha