[英]how to subtract the column values of two tables when there is no record in second query
在我的學校申請書中,我想知道學生的應有數量,以便在我的
fee_class_mapping表我將費用分配給學生的班級,如下所示
+----------------------+------------+----------+------------+
| fee_class_mapping_id | fee_cat_id | class_id | fee_amount |
+----------------------+------------+----------+------------+
| 1 | 2 | 1 | 10000 |
| 2 | 2 | 2 | 15000 |
| 3 | 3 | 1 | 2000 |
+----------------------+------------+----------+------------+
在student_present_class_details表中,我會有學生班級信息
+----------------------+----------+
| student_admission_id | class_id |
+----------------------+----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 1 |
| 4 | 1 |
+----------------------+----------+
通過映射student_present_class_details class_id
和fee_class_mapping class_id
我將獲得分配給學生多少費用
還有另一個表格,其中包含fee_category詳細信息
+------------+--------------+-----------+
| fee_cat_id | fee_cat_name | parent_id |
+------------+--------------+-----------+
| 1 | School Fee | 0 |
| 2 | School Fee | 1 |
| 3 | books | 1 |
| 4 | Dhobi | 0 |
+------------+--------------+-----------+
我將通過以下查詢獲得特定學生總費用的fee_category明智金額
select f.fee_cat_name, sum(fcm.fee_amount) from fee_class_mapping fcm ,student_present_class_details pd ,fee_category f
where pd.class_id=fcm.class_id and f.fee_cat_id=fcm.fee_cat_id and pd.student_admission_id=4 group by fcm.fee_cat_id;
淘汰
+--------------+---------------------+
| fee_cat_name | sum(fcm.fee_amount) |
+--------------+---------------------+
| School Fee | 10000 |
| books | 2000 |
+--------------+---------------------+
我還有另一個稱為Fee_transactions的表,其中我將按學生費用類別明細處理交易明細,表結構為
+---------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+------------------+------+-----+---------+----------------+
| transaction_id | int(11) | NO | PRI | NULL | auto_increment |
| student_id | int(11) | NO | MUL | NULL | |
| fee_cat_id | int(11) | NO | MUL | NULL | |
| amount_paid | varchar(255) | YES | | NULL | |
| paid_on | date | YES | | NULL | |
| trans_receipt_no | varchar(255) | YES | | NULL | |
| is_concession_given | enum('Yes','No') | YES | | NULL | |
| payment_mode | varchar(45) | YES | | NULL | |
| refrence_number | varchar(45) | YES | | NULL | |
| tdm_id | int(11) | YES | | NULL | |
+---------------------+------------------+------+-----+---------+----------------+
從上表中,我將按以下方式獲得已支付費用類別的總和:
select sum(ft.amount_paid) from fee_transactions ft where ft.student_id=4 group by ft.fee_cat_id;
淘汰
+---------------------+
| sum(ft.amount_paid) |
+---------------------+
| 3000 | //school fee
+---------------------+
我要減去總金額-已付金額
所以我寫了以下查詢
select f.fee_cat_name, sum(fcm.fee_amount)-(select sum(ft.amount_paid) from fee_transactions ft where ft.student_id=4 and ft.fee_cat_id=fcm.fee_cat_id group by ft.fee_cat_id) from fee_class_mapping fcm ,student_present_class_details pd ,fee_category f
where pd.class_id=fcm.class_id and f.fee_cat_id=fcm.fee_cat_id and pd.student_admission_id=4 group by fcm.fee_cat_id;
輸出
+--------------+------------+
| fee_cat_name | paidAmount |
+--------------+------------+
| School Fee | 7000 |
| books | NULL |
+--------------+------------+
在fee_transaction表中,我沒有書費條目,有學費條目
總學費= 10000-支付的金額3000 = 7000是正確的
對於圖書費用,我沒有在fee_transaction表中記錄,因此其未返回值
我想要的是
+--------------+------------+
| fee_cat_name | paidAmount |
+--------------+------------+
| School Fee | 7000 |
| books | NULL | //2000 instead of NULL
+--------------+------------+
我將如何實現它。
請幫幫我。 謝謝!!。
在子查詢的結果中,使用coalesce()
將NULL
替換為0。
SELECT f.fee_cat_name,
sum(fcm.fee_amount) - coalesce((SELECT sum(ft.amount_paid)
FROM fee_transactions ft
WHERE ft.student_id = 4
AND ft.fee_cat_id = fcm.fee_cat_id
GROUP BY ft.fee_cat_id), 0)
FROM fee_class_mapping fcm
INNER JOIN student_present_class_details pd
ON pd.class_id = fcm.class_id
INNER JOIN fee_category f
ON f.fee_cat_id = fcm.fee_cat_id
WHERE pd.student_admission_id = 4
GROUP BY fcm.fee_cat_id;
我還重寫了它以使用顯式的JOIN
語法,建議使用該語法,因為它更易於閱讀和理解。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.