Mysql
GROUP BY 列的總和,在 GROUP BY 中
我想顯示行 GROUP BY 主要參考 ID,然後顯示金額 GROUP BY 類型。
這是我的查詢
SELECT gl.`drcr`,gl.`amount`,gl.`name`,gl.`type`, gl.`narration`,gl.`main_reference`,gl.`date` " . "FROM `general_ledger` AS gl " . "INNER JOIN ( " . " SELECT SUM(amount) AS total FROM `general_ledger` GROUP BY `type` ) AS tot " . "WHERE gl.`date` BETWEEN '$start_date' AND '$end_date' " . "GROUP BY gl.`main_reference`,gl.`date`,gl.`name`, gl.`type`,gl.`amount`,gl.`drcr`,gl.`narration`
SELECT `drcr`,SUM(`amount`) `amount`,`name`,`type`,`narration`,`main_reference`,`date` FROM ( SELECT gl.`drcr`,gl.`amount`,gl.`name`,gl.`type`,gl.`narration`,gl.`main_reference`,gl .`date` " . "FROM `general_ledger` AS gl " . "INNER JOIN ( " . " SELECT SUM(amount) AS total FROM `general_ledger` GROUP BY `type` ) AS tot " . "WHERE gl.`date` BETWEEN '$start_date' AND '$end_date' " . "GROUP BY gl.`main_reference`,gl.`date`,gl.`name`,gl.`type`,gl.`amount`,gl.`drcr`,gl.`narration` ) Interimresults GROUP BY `drcr`, `name`,`type`,`narration`,`main_reference`,`date`