Mysql 选择表中不存在的记录,但也包括那些存在where子句排除的记录

Mysql 选择表中不存在的记录,但也包括那些存在where子句排除的记录,mysql,sql,mariadb,Mysql,Sql,Mariadb,我有两层。一个级别适用于得分至少为1分的客户,另一个级别适用于得分至少为1000分的客户 我想获得积分>=950和

我有两层。一个级别适用于得分至少为1分的客户,另一个级别适用于得分至少为1000分的客户

我想获得积分>=950和<1000的客户的记录。我计划把他们转移到第二层作为奖励。我最近导入了客户,即使他们至少得了1分,他们也不会自动添加到第一层

当我进行查询时,它不包括我导入的那些,因为它们不存在于customer\u vip\u tiers表中

我的理解是,由于cvt,他们无法获得不在该层的记录。tier_id!=1734子句,因为他们在customers\u vip\u tiers表中没有记录。我怎样才能得到那些表中没有记录的呢?

我得到了答案

我摆脱了cvt。tier_id!=1734年


您的vip_tiers_设置表中有这些客户吗?没有。这只是各层的设置@User3315556那么,您已经在customer表中导入了客户了吗?如果有,他们有商户id吗?@user3315556是的,他们有商户id。我认为一个解决方案是,您可以将客户c上的左连接更改为右连接。
tier 1 id = 1733
tier 2 id = 1734
select
       c.id,
       c.email,
       sum(p.reward_points) as total_points,
       vt.name,
       cvt.tier_id

from vip_tiers_settings vts
left join customers c on c.merchant_id = vts.merchant_id
left join customers_vip_tiers cvt on c.id = cvt.customer_id
left join vip_tiers vt on vt.id=cvt.tier_id
left join perks p on p.customer_id = c.id
                        and p.created_at >= coalesce(vts.custom_start_date,0)
                        and p.completed = 1
                        and p.reversed = 0
                        and p.expired = 0

where
c.merchant_id = 50506
and cvt.tier_id != 1734

group by c.id having sum(p.reward_points) >= '950' and sum(p.reward_points) < '1000'

;
select
       c.id,
       c.email,
       sum(p.reward_points) as total_points,
       vt.name,
       cvt.tier_id

from vip_tiers_settings vts
left join customers c on c.merchant_id = vts.merchant_id
left join customers_vip_tiers cvt on c.id = cvt.customer_id
left join vip_tiers vt on vt.id=cvt.tier_id
left join perks p on p.customer_id = c.id
                        and p.created_at >= coalesce(vts.custom_start_date,0)
                        and p.completed = 1
                        and p.reversed = 0
                        and p.expired = 0

where
c.merchant_id = 50506
and c.id not in(select customer_id from customers_vip_tiers cvv where cvv.merchant_id=50506 and cvv.tier_id=1734)

group by c.id having sum(p.reward_points) >= '950' and sum(p.reward_points) < '1000';