thekucays
thekucays

Reputation: 618

mysql join how to select data null

i'm having few tables with structure like this

Promo_Validation

id | promo_id | validation_type | validation_value

Promo_Counter

id | promo_id | tanggal | counter | member_id | mid

Promo_Dtl

id | promo_id | min_amount_trx | max_amount_trx | cashback_type | cashback_value | max_cashback_value

Promo_Participants

id | promo_id | applied_to | created_date

Btw, these tables are for promo mechanism..
So the promo_counter table will count users that already had promo transactions.. if they haven't completed promo transactions yet, no record created on that table
The promo_validation and promo_dtl table is where promo terms and conditions saved
And promo_participants table is where merchants that joined the promo saved


So, i'm trying to create a query that joined them all, i want to show promo mechanism datas along with counting for each users (how many times the users use the current promo etc).. My query looks like this

select pp.id as pp_id, pv.id as pv_id, pv.validation_type as pv_validationtype, pv.validation_value as pv_validationvalue,
pc.id as pc_id, pc.tanggal as pc_tanggal, pc.counter as pc_counter, pc.member_id as pc_memberid, pc.mid as pc_mid,
pd.min_amount_trx as pd_minamounttrx, pd.max_amount_trx as pd_maxamounttrx, pd.cashback_type as pd_cashbacktype, pd.cashback_value as pd_cashbackvalue, pd.max_cashback_value as pd_maxcashbackvalue
    from emoney_promo.promo_validation pv
    join emoney_promo.promo_counter pc on pv.promo_id = pc.promo_id
    join emoney_promo.promo_dtl pd on pd.promo_id = pv.promo_id
    join emoney_promo.promo_participants pp on pp.promo_id = pv.promo_id
    where pc.member_id = '0867667762'
    and pp.applied_to = '4518'
    and pc.mid = '4518';

The query works when users (on member_id column) already have data on emoney_promo.promo_counter table. But when user is not available in promo_counter table (that user haven't completed the promo transactions yet), the query returns nothing..

Is there any way so if the user have not completed any promo yet (in query above, so there's no pc.member_id = 0867667762, i can still get the results? I mean, just give null to the columns that have no results (from promo_counter table), instead of returns nothing?

Sorry if my words kinda complicated btw :)

Upvotes: 0

Views: 54

Answers (1)

Umesh Adtani
Umesh Adtani

Reputation: 90

How about this?

select pp.id as pp_id, pv.id as pv_id, pv.validation_type as pv_validationtype, pv.validation_value as pv_validationvalue,
pc.id as pc_id, pc.tanggal as pc_tanggal, pc.counter as pc_counter, pc.member_id as pc_memberid, pc.mid as pc_mid,
pd.min_amount_trx as pd_minamounttrx, pd.max_amount_trx as pd_maxamounttrx, pd.cashback_type as pd_cashbacktype, pd.cashback_value as pd_cashbackvalue, pd.max_cashback_value as pd_maxcashbackvalue
    from emoney_promo.promo_validation pv
    left join emoney_promo.promo_counter pc on pv.promo_id = pc.promo_id and pc.mid = '4518' and pc.member_id = '0867667762'
    join emoney_promo.promo_dtl pd on pd.promo_id = pv.promo_id
    join emoney_promo.promo_participants pp on pp.promo_id = pv.promo_id
    where pp.applied_to = '4518';

Upvotes: 2

Related Questions