我使用以下查詢來獲取不同的表我的數據庫,檢查報告如下......MySQL的INNER JOIN
SELECT s.id, s.name, c.name AS course_name,
s.open_bal AS open_balance, sum(i.amount) AS gross_fee,
sum(i.discount) AS discount, sum(i.amount) - sum(i.discount) AS net_payable,
SUM(r.reg_fee+r.tut_fee+r.other_fee) AS net_recieved,
(sum(i.amount) - sum(i.discount)) - SUM(r.reg_fee+r.tut_fee+r.other_fee) AS balance_due
FROM students s
INNER JOIN courses c on c.id = s.course_id
LEFT JOIN invoices i on i.student_id = s.id
LEFT JOIN recipts r on r.student_id = s.id;
發票
| id | student_id | amount | discount | dnt |
+----+------------+----------+----------+-------------+
| 2 | 22 | 35000 | 0 | 2011/01/01 |
+----+------------+----------+----------+-------------+
沒有得到來自gross_fee和net_payable正確的值。
謝謝。
GROUP BY s.id也許? – 2011-04-13 11:45:35
你能改變你的代碼嗎? – seoppc 2011-04-13 11:48:19
嘗試將'INNER'改爲'LEFT'加入並分享結果 – Teneff 2011-04-13 11:50:21