[英]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 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 因此,
promo_counter
表将对已经进行促销交易的用户进行计数..如果他们尚未完成促销交易,则不会在该表上创建任何记录
The promo_validation
and promo_dtl
table is where promo terms and conditions saved promo_validation
和promo_dtl
表是保存促销条款和条件的位置
And promo_participants
table is where merchants that joined the promo saved 而
promo_participants
表是加入促销的商家保存的位置
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. 当用户(在member_id列上)已经在
emoney_promo.promo_counter
表上具有数据时,该查询有效。 But when user is not available in promo_counter table (that user haven't completed the promo transactions yet), the query returns nothing.. 但是,当promo_counter表中没有该用户可用时(该用户尚未完成促销交易),该查询将不返回任何内容。
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? 有什么办法,如果用户还没有完成任何促销(在上面的查询中,那么就没有
pc.member_id = 0867667762
,我仍然可以获得结果吗?我的意思是,将没有结果的列设为null
(来自promo_counter表),而不返回任何内容?
Sorry if my words kinda complicated btw :) 抱歉,我的话有点复杂,顺便说一句:)
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';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.