如果taameer_pay_details表中有相应的日期记录,则以下查询给出记录的输出。如果没有相应的记录,查询将隐藏fund_type_master
中的基金名称select Revenue as Revenue,
cur_date as Date,
rec_no as FirstRecNo,
Cash as cash,
cheque as cheque,
sum(Cash+ cheque) as Total
from (SELECT
ft.fund_type as Revenue,STR_TO_DATE(tpd.paid_date,'%d-%m-%Y') as cur_date,tpd.rec_no as rec_no,sum(if(tpd.pay_mode='C',tpd.paid_amt,0)) as Cash,sum(if(tpd.pay_mode='DD',tpd.paid_amt,0)) as cheque
FROM taameer_details fd
JOIN taameer_pay_details tpd
on tpd.tpay_id=fd.tamDet_id
cross join fund_type_master ft
on fd.fund_type =ft.fund_id
where TIMESTAMPDIFF(DAY , STR_TO_DATE(tpd.paid_date, '%d-%m-%Y') ,'2015-02-02') >=0
AND TIMESTAMPDIFF(DAY ,'2015-01-01',STR_TO_DATE(tpd.paid_date, '%d-%m-%Y') ) >=0
AND tpd.pay_type=2
AND tpd.status=1
group by ft.fund_id) a
group by Revenue
预期产出
Revenue Date FirstRecNo cash cheque Total
General Funds 2015-01-16 0 46100 7800 53900
Voluntary 2015-01-16 0 0 0 0
XYz 2015-01-16 0 0 0 0
Muv 2015-01-16 0 0 0 0
HMjwa 2015-01-16 0 0 900 900
NJUMz 2015-01-16 0 0 0 0
S Unit 2015-01-16 0 251 0 251
当前输出
Revenue Date FirstRecNo cash cheque Total
General Funds 2015-01-16 0 46100 7800 53900
NJUMz 2015-01-16 0 0 900 900
S Unit 2015-01-16 0 251 0 251
请帮我重新编写查询,以便按照预期结果输出数据。
答案 0 :(得分:0)
尝试使用外部联接:
FROM taameer_details fd
LEFT OUTER JOIN taameer_pay_details tpd
您还应该在" on子句"中包含联接表taameer_pay_details的所有条件。外连接:
FROM taamer_details fd
LEFT OUTER JOIN taameer_pay_details tpd
ON tpd.tpay_id=fd.tamDet_id
AND TIMESTAMPDIFF(DAY , STR_TO_DATE(tpd.paid_date, '%d-%m-%Y') ,'2015-02-02') >=0
AND ...