【问题标题】:Codeigniter SUM and LEFT JOIN while removing duplicatesCodeigniter SUM 和 LEFT JOIN,同时删除重复项
【发布时间】:2026-01-09 17:00:02
【问题描述】:

我只剩下加入 4 个表来获得第一个表中存在的金额的总和。到目前为止,我已经尝试了许多替代方法来直接计算总和,但是如果我不使用 group_by,它要么变为 0(如果我使用 group_by),要么重复。这是我的代码:

$this->db->select('COALESCE(SUM(cust_ reservation_request.dollar_amount), "0") as total');
        $this->db->join('cust_ reservation_request', 'cust_ reservation_request.id = cust_reservations.rid', 'LEFT');
        $this->db->join('attendees', 'cust_rid= cust_ reservation_request.id', 'LEFT');
        $this->db->join('cust_reservation_cancelled', 'cust_reservation_cancelled.rid = cust_ reservation_request.id', 'LEFT');
        $this->db->where('cust_ reservation_request.tid IN (5, 6, 7, 8)->where('attendees.status', 1)->where('cust_reservation_cancelled.rid IS NULL');
        $this->db->group_by('cust_reservation_request.id');
        $total = $this->db->get('cust_reservations')->row()->total;

我得到 0 作为 $total 变量的值,而它应该是 288。 但是,当我删除 $this->db->group_by('cust_reservation_request.id'); 语句时,我在 attendees.attendees.cust_rid 上得到了重复,实际上它在那里,$total 变为 498,因为可能有多个参加者进行相同的摆脱。我想删除这个重复项,保留所有 Dollar_amount 字段的实际总和。

我的桌子是:

cust_ reservation_request
id   // index, foreign key in rest of other tables
tid
uid
dollar_amount

cust_reservations
id
rid  // related to id in cust_ reservation_request table
reservation_date
pay_token

cust_ reservation_cancelled
id
rid  // related to cust_ reservation_request.id
cancel_date

attendees
id
cust_rid // its the same rid, related to cust_reservation_request.id
status

您认为有没有更安全的方法可以做到这一点,在以适当的方式进行 SUM 的同时删除重复项?

提前感谢您的帮助。

【问题讨论】:

  • 尝试在没有“WHERE cust_reservation_cancelled.rid IS NULL”条件的情况下运行它
  • 你可以在不处理框架的情况下在服务器上将它作为 SQL 查询运行吗?这样调试起来会更容易
  • 嗨@IgorM 感谢您的回复。我已经尝试在服务器上运行它,当我添加 group_by 时它返回 0,并在使用 SUM 后返回重复的行。我也尝试使用 SELECT * 而不是 SUM 只是为了查看返回的结果,我知道它在那里返回了两次。
  • Hii @IgorM 我尝试在没有“WHERE cust_reservation_cancelled.rid IS NULL”的情况下运行查询,但结果保持不变。
  • 在没有 COALESCE 和任何 WHERE 条件的情况下运行它。

标签: mysql sql codeigniter sum left-join


【解决方案1】:

我最初的回答是:“我认为你的数据有问题,查询返回 NULL,它被 COALESCE 转换为 0”,但这不是问题。 “attendees”表中的“status”字段有问题,主要由topic starter解决,所以我这里不值得分

Zafar,很高兴您设法解决了问题。来自以色列的问候:)

【讨论】:

  • 正如我已经解释的那样,我尝试在没有 'COALESCE, by just putting this in select: SELECT SUM(user_reservation_request.dollar_amount) 的情况下运行它,但它没有工作。它又给我发了 0。
  • 如果我的数据有问题,当我删除 group_by 时,它不应该返回一些金额!它返回一些金额,但有一个重复的条目,当我在 mySQL 中运行它时我可以看到它。
  • @Zafar,你说运行以下查询得到 0 SELECT SUM(dollar_amount) FROM cust_reservation_request...你不觉得你的数据类型有问题吗?
  • 看看,如果我运行这个查询:SELECT SUM(cust_ reservation_request.dollar_amount) as total FROM cust_reservations` LEFT JOIN cust_ reservation_request ON cust_reservation_request.id = cust_reservations.rid LEFT JOIN attendees ON @98 @.cust_id = cust_ reservation_request.id LEFT JOIN user_reservation_cancelled ON user_reservation_cancelled.rid = user_reservation_request.id WHERE cust_ reservation_request.@98765439,98765439, 16,17) AND attendees.status = 1` 没有额外的 where,它返回 498。那么数据类型有什么问题?
  • 如您所见,您按“user_reservation_request.tid”过滤。可能它会过滤掉尴尬的记录。什么是 Dollar_amount 数据类型?
【解决方案2】:

可能是这样的... (翻译成大型机上的情况)

应该是这样的:

    $this->db->select('COALESCE(SUM(cust_ reservation_request.dollar_amount), "0") as total');
            $this->db->where exists (select 1 from cust_reservations where  cust_reservations.rid = cust_ reservation_request.id);
            $this->db->and exists (select 1 from attendees where cust.rid = cust_reservation_request.id); 
            $this->db->and exists (select 1 from cust_ reservation_cancelled where cust_reservation_cancelled.rid = cust_reservation_request.id);
            $this->db->and ('cust_ reservation_request.tid IN (5, 6, 7, 8)->where('attendees.status', 1)->where('cust_reservation_cancelled.rid IS NULL');
            $this->db->group_by('cust_reservation_request.id');
            $total = $this->db->get('cust_reservations')->row()->total;

您必须检查语法以及所有 () 和 ' 并且因为我不知道除了普通 SQL 之外的语言,您可能必须在 'where exists' 中更改 'and exists' 和/或将 where 更改为一个和。

此查询(在普通 SQL 中)的目的是您只想从一个表(cust_reservation_request)中求和美元金额,您希望确保在其他表中也存在具有相同 row_id 的行。

希望这会有所帮助。

【讨论】:

  • 感谢您的评论@BenLink,但是,正如 IgorM 已经解释的那样,我通过子查询解决了它,然后通过全局应用 where attendees.status =1 子句。不过,我很欣赏你的努力。谢谢。