HiDayurie Dave HiDayurie Dave - 6 months ago 13
MySQL Question

MySQL Query 2 Tables Join Column beside

I have 2 tables, called

tb_compulsory_savings
and
tb_time_deposits


for
tb_compulsory_savings


form_no | badge_id | compulsory_savings | transaction_date
1 | 090802 | 50000 | 2016-05-20 13:10:11
2 | 090801 | 50000 | 2016-05-20 13:15:15


for
tb_time_deposits


form_no | badge_id | time_deposits | transaction_date | period | status
1 | 090802 | 100000 | 2016-05-20 13:20:44 | May-2016 | closed


Now I want to query that 2 table to be like this:

badge_id | compulsory_savings | time_deposits
090802 | 50000 | 100000
090801 | 50000 | null


So, if
badge_id: 090801
is not existed in
tb_time_deposits
, it will display null.

I have tried LEFT JOIN, RIGHT JOIN, UNION but still the display is not as I want.

Answer

Looks like you're looking for a left join:

SELECT    cs.badge_id, cs.compulsary_savings, td.time_deposits
FROM      tb_compulsory_savings cs
LEFT JOIN tb_time_deposits td ON cs.badge_id = td.badge_id