简体   繁体   English

我正在尝试加入其中一个表中不存在用户记录的表

[英]I am trying to join tables where a user record does not exist in one of the tables

I have a rewards program with a grants and a purchases table.我有一个带有赠款和购买表的奖励计划。 I am attempting to create a report that displays a users balance.我正在尝试创建一个显示用户余额的报告。 If the user has not made a purchase, however, the balance report does not contain their record.但是,如果用户尚未购买,余额报告将不包含他们的记录。 The report only contains users who have earned grants AND made at least one purchase.该报告仅包含已获得赠款并至少进行过一次购买的用户。

The third table being used is our user records table to pull in the user record.使用的第三个表是我们的用户记录表,用于拉入用户记录。

Is there a way to treat no record in the purchases table as a '0' balance?有没有办法将购买表中的无记录视为“0”余额?

Could you not have an entry for every user with a 0 balance?您不能为每个余额为 0 的用户设置一个条目吗? That is the reason this wont work.这就是这行不通的原因。 You are asking the database to join/display something that does not exist.您要求数据库加入/显示不存在的内容。

Without the join, you can display no records as 0 but you won't be able to create a join to nothing so I suggest you add an entry for each user (when the users account is created) in the purchases tablet with a 0 balance.如果没有加入,您可以将任何记录显示为 0,但您将无法创建一个无任何连接,因此我建议您在购买平板电脑中为每个用户(创建用户帐户时)添加一个条目,余额为 0 .

Hope this helps希望这可以帮助

Something like this should work:这样的事情应该工作:

CASE WHEN (SELECT COUNT(*) FROM purchases WHERE purchase_user_id = user_id) > 0
     THEN balance_value
     ELSE 0
 END as balance

EDIT: Going by the query that you added, I think you can just COALESCE the amount to 0 before doing the SUM() .编辑:根据您添加的查询,我认为您可以在执行SUM()之前将金额COALESCE为 0 。 This should prevent returning a NULL value if there are no records in the purchases table.如果购买表中没有记录,这应该可以防止返回 NULL 值。

SELECT ce.platform_id,
       SUM(COALESCE(p.amount, 0))
FROM current_eligibility ce
LEFT JOIN purchases p ON p.platformId = ce.platform_id
WHERE p.currency = 'X123' AND
      ce.platform_id = 'X2334'
GROUP BY ce.platform_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用MySQL使用LEFT JOIN返回结果,其中一个表不包含记录 - Using MySQL to return results using LEFT JOIN where one of the tables does not contain a record 我有2个表,并且试图随机选择另一个表中不存在的值,但我无法使其正常工作 - I have 2 tables and am trying to select at random a value that does not exist in the other table but I cant get it to work 尝试左连接3个ID可能相同的表,我通过类型定义它 - Trying to left join 3 tables where the id could be the same, I am defining it via the type 我正在尝试将4个表与INNER JOIN链接在一起 - I am trying to link 4 tables together with INNER JOIN 使用左联接联接多个表(其中联接的表之一中不存在条目)不返回任何内容 - Joining multiple tables with left join where an entry doesn't exist in one of the joined tables returns nothing 加入id可能不存在的多个表 - Join multiple tables where id may not exist 我正在创建两个表,在其中一个表中,我试图分配一个外键,但它给了我一个错误 - I am Creating two tables and in one of the tables I am trying to assign a foreign key but it is giving me an error SELECT JOIN WHERE一条记录不存在 - SELECT JOIN WHERE a record does not exist 如何获取不存在联接的记录? - How to get record where join does not exist? Codeigniter 3连接表并获得一条记录 - codeigniter 3 join tables and get one record
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM