2011-10-12 167 views
2

可能重複:
complex my sql query wrong resultsMySQL查詢幫助需要

我試圖構建複雜的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  | 
+------------+-----------------+----------+-----------+----------+-------------+--------------+-------------+ 

請檢查並回復,謝謝。

+1

精確轉貼。請不要轉發您近期詢問的問題,@seoppc。謝謝。 –

+0

@Jordan是的,我創造了新的問題。我真的很困惑,請幫助。 – seoppc

回答

0

您必須將student_id添加到GROUP BY表達式中。

您正在創建多個外連接,這意味着所有數據將用於查詢中,但收據表中的某些學生也有多行,因此它們的金額會被複制(因爲會有與該s.id幾行)。

採取以下加入一看:

 r.id  s.id  open_bal 
     -------------------------- 
     8  | 44 | -16000 
     277  | 44 | -16000 
     26  | 182 | 9000 
     424  | 182 | 9000 
     468  | 182 | 9000 
     36  | 185 | 19000 
     697  | 185 | 19000 
     66  | 187 | 4000 
     null | 184 | -36000 
     null | 186 | 9000 

概括起來,你會被前面的問題(以上)的同一用戶獲得10萬

+0

可以請你發佈sql查詢嗎?如果我寫GROUP BY r.student_id,b.name,b.id然後我得到的所有數據由r.id分組而不是b.name – seoppc