mysql join如何选择数据null

时间:2017-06-13 09:31:21

标签: mysql sql

我有几张像这样的结构的表

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

顺便说一句,这些表是促销机制..
因此,promo_counter表将计算已经有促销交易的用户..如果他们尚未完成促销交易,则不会在该表上创建记录
promo_validationpromo_dtl表是宣传促销条款和条件的地方
promo_participants表是加入促销活动的商家保存的地方


所以,我试图创建一个加入它们的查询,我想显示促销机制数据以及每个用户的计数(用户使用当前促销的次数等)。我的查询看起来像这样< / p>

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';

当用户(在member_id列上)已有emoney_promo.promo_counter表上的数据时,查询有效。但是当用户在promo_counter表中不可用时(用户尚未完成促销交易),查询将不返回任何内容。

如果用户还没有完成任何促销活动,那么有什么办法(在上面的查询中,所以没有pc.member_id = 0867667762,我仍然可以得到结果吗?我的意思是,只是给{{1没有结果的列(来自promo_counter表),而不是什么都不返回?

对不起,如果我的话有点复杂btw:)

1 个答案:

答案 0 :(得分:2)

这个怎么样?

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';