我有三个名为"salary"
,"allowance"
和"bill"
的表格。
"salary"
表有
s_id [primary key], E_ID, S_Amount, S_Date.
"allowanace"
表有
A_ID[primary key], E_ID, A_TA, A_DA, A_MA, A_Others, A_Date, Total_A
"bill"
表有
E_ID[primary key], Electric, Gas, Water, B_Others, B_Date Total_B
我已使用此查询>>
加入这些表格 SELECT
SUM(allowance.`A_TA`+allowance.`A_DA`
+allowance.`A_MA`+allowance.`A_Others`)
AS Total_Allowance,
(SUM(salary.`S_Amount`)) AS Total_Salary,
SUM(bill.`Electric`+bill.`Water`+bill.`Gas`+bill.`B_Others`) as
Total_Bill,
(SUM(allowance.`A_TA`+allowance.`A_DA`+allowance.`A_MA`
+allowance.`A_Others`)+SUM(salary.`S_Amount`)
+SUM(bill.`Electric`+bill.`Water`+bill.`Gas`+bill.`B_Others`))
AS Total_Ex
from salary
INNER JOIN allowance
ON salary.E_ID=allowance.E_ID
INNER JOIN bill
ON salary.S_Date=bill.B_Date
我想获得2015年1月,2015年12月,2016年10月,2016年11月的总费用“Total_Ex”...每个月的总费用。
答案 0 :(得分:0)
这会有用吗? SELECT转换(varchar(8),bill.B_Date,120)+'01',SUM(allowance.A_TA + allowance.A_DA + allowance.A_MA + allowance.A_Others)AS Total_Allowance, (SUM(salary.S_Amount))AS Total_Salary,SUM(bill.Electric + bill.Water + bill.Gas + bill.B_Others)为Total_Bill, (SUM(allowance.A_TA + allowance.A_DA + allowance.A_MA + allowance.A_Others)+ SUM(salary.S_Amount)+ SUM(bill.Electric + bill.Water + bill.Gas + bill.B_Others)) AS Total_Ex 从薪水 INNER JOIN津贴 ON salary.E_ID = allowance.E_ID INNER JOIN法案 ON salary.S_Date = bill.B_Date 按转换分组(varchar(8),bill.B_Date,120)+'01'
答案 1 :(得分:0)
尝试使用SUM和IF:
SUM(IF(DATE_FORMAT(salary.S_Date,'%M')='January',
allowance.A_TA+allowance.A_DA+allowance.A_MA
+allowance.A_Others
+salary.S_Amount+bill.Electric+bill.Water
+bill.Gas+bill.B_Others,0)) AS
Jan_Total_Ex
这将为您提供Jan总费用。