I would like to know the alternative for Full Outer Join in MySQL. I know it can be done through union but it's not working because my requirement is little complex I hope so. I have two tables master(branch_id,purchase_mindate,purchase_billvalue) and transfer(branch_id,tra_gtr_date,tra_travalue) in which both id fields will be almost same. Now I need to get the values id,sum(billvalue),sum(travalue) by joining two tables with the conditions
- Mindate=current date
- Gtrdate=current date
- If master's table doesn't have any value but transfer table contains then transfer's value must be included
- If master's table contains value but transfer doesn't then master's value must be displayed.
I tried union, join, etc, but nothing helped I know I am doing something wrong somewhere but I can't identify that please help me with this.
I used the below query,
select purchase_master.branch_id, sum(purchase_billvalue) as billvalue, sum(tra_value) as travalue
from purchase_master
join purchase_transfer on purchase_master.branch_id=purchase_transfer.branch_id
where purchase_mindate=CURDATE() and tra_gtr_date=CURDATE()
group by branch_id
I get values only if two tables contains the current date but I don't get any values if one table has and other doesn't
I have attached Screen shots of tables.
Master Table
Transfer Table
I tried the below query but the result is empty,
select pm.branch_id,pm.purchase_billvalue, pt.tra_value from purchase_master as pm
left outer join purchase_transfer as pt on pm.branch_id=pt.branch_id
where pm.purchase_mindate=CURDATE() and pt.tra_gtr_date=CURDATE()
union
select pm.branch_id,pm.purchase_billvalue, pt.tra_value from purchase_master as pm
right outer join purchase_transfer as pt on pm.branch_id = pt.branch_id
where pm.purchase_mindate=CURDATE() and pt.tra_gtr_date=CURDATE()
I need the result as sum(billvalue) sum(travalue) group by branch_id,
- If two tables contains currentdate data
- If any one contains the currentdate data