我試圖構建複雜的MySQL查詢,但其返回錯誤結果...
SELECT b.name AS batch_name, b.id AS batch_id,
COUNT(DISTINCT s.id) AS total_students,
COALESCE(sum(s.open_bal), 0) AS open_balance,
sum(COALESCE(i.reg_fee,0) + COALESCE(i.tut_fee,0) + COALESCE(i.other_fee,0)) AS gross_fee,
sum(COALESCE(i.discount,0)) AS discount,
COALESCE(sum(s.open_bal), 0) + sum(COALESCE(i.reg_fee,0) + COALESCE(i.tut_fee,0) + COALESCE(i.other_fee,0)) - sum(COALESCE(i.discount,0)) AS net_payable,
sum(COALESCE(r.reg_fee,0) + COALESCE(r.tut_fee,0) + COALESCE(r.other_fee,0)) AS net_recieved,
(COALESCE(sum(s.open_bal), 0) + sum(COALESCE(i.reg_fee,0) + COALESCE(i.tut_fee,0) + COALESCE(i.other_fee,0)) - sum(COALESCE(i.discount,0))) - (sum(COALESCE(r.reg_fee,0) + COALESCE(r.tut_fee,0) + COALESCE(r.other_fee,0))) AS balance_due
FROM batches b
LEFT JOIN students s on s.batch = b.id
LEFT JOIN invoices i on i.student_id = s.id
LEFT JOIN recipts r on r.student_id = s.id
WHERE s.inactive = 0
GROUP BY b.name, b.id;
我是因爲sum(open_bal)結果是錯誤的,所以做錯了。
樣品結果...
| batch_name | total_students | open_bal | gross_fee | discount | net_payable | net_recieved | due_balance |
+------------+-----------------+----------+-----------+----------+-------------+--------------+-------------+
| MS | 6 | 10000 | 0 | 0 | 10000 | 101000 | -91000 |
+------------+-----------------+----------+-----------+----------+-------------+--------------+-------------+
以上結果是錯誤的,請檢查下面的表結構
學生表
| id | open_bal | batch |
+-----+----------+-------+
| 44 | -16000 | 9 |
+-----+----------+-------+
| 182 | 9000 | 9 |
+-----+----------+-------+
| 184 | -36000 | 9 |
+-----+----------+-------+
| 185 | 19000 | 9 |
+-----+----------+-------+
| 186 | 9000 | 9 |
+-----+----------+-------+
| 187 | 4000 | 9 |
+-----+----------+-------+
發票表
| id | student_id | reg_fee | tut_fee | other_fee | net_payable | discount |
+------+------------+---------+---------+-----------+-------------+----------+
| | | | | | | |
+------+------------+---------+---------+-----------+-------------+----------+
沒有發票可用於以上學生ID。
Recipts表
| id | student_id | reg_fee | tut_fee | other_fee | status |
+------+------------+---------+---------+-----------+------------+
| 8 | 44 | 0 | 0 | 1500 | confirmed |
+------+------------+---------+---------+-----------+------------+
| 277 | 44 | 0 | 50000 | 0 | confirmed |
+------+------------+---------+---------+-----------+------------+
| 26 | 182 | 0 | 0 | 1500 | confirmed |
+------+------------+---------+---------+-----------+------------+
| 424 | 182 | 0 | 15000 | 0 | confirmed |
+------+------------+---------+---------+-----------+------------+
| 468 | 182 | 0 | 15000 | 0 | confirmed |
+------+------------+---------+---------+-----------+------------+
| 36 | 185 | 0 | 0 | 1500 | confirmed |
+------+------------+---------+---------+-----------+------------+
| 697 | 185 | 0 | 15000 | 0 | confirmed |
+------+------------+---------+---------+-----------+------------+
| 66 | 187 | 0 | 0 | 1500 | confirmed |
+------+------------+---------+---------+-----------+------------+
使用上面的SQL查詢和表格預期結果...
| batch_name | total_students | open_bal | gross_fee | discount | net_payable | net_recieved | due_balance |
+------------+-----------------+----------+-----------+----------+-------------+--------------+-------------+
| MS | 6 | -11000 | 0 | 0 | 10000 | 101000 | -112000 |
+------------+-----------------+----------+-----------+----------+-------------+--------------+-------------+
請檢查並回復,謝謝。
精確轉貼。請不要轉發您近期詢問的問題,@seoppc。謝謝。 –
@Jordan是的,我創造了新的問題。我真的很困惑,請幫助。 – seoppc