使用SUM()将MySQL查询语句转换为Codeigniter活动记录样式查询语句

时间:2012-11-02 04:03:09

标签: php mysql codeigniter activerecord

我试图找出转换这个mysql查询的最佳方法

SELECT
        SUM(invite.friendID = $mID AND invite.decidedwhen = '0000-00-00 00:00:00' AND evnt.ends >= '$event_ts' and invite.isactive = 0 and evnt.isactive = 0) AS invites_undecided,
        SUM(invite.friendID = $mID AND invite.decidedwhen != '0000-00-00 00:00:00' AND invite.yes = 1 AND evnt.ends >= '$event_ts' and invite.isactive = 0) and evnt.isactive = 0 AS invites_yes,
        SUM(invite.friendID = $mID AND invite.decidedwhen != '0000-00-00 00:00:00' AND invite.no = 1 AND evnt.ends >= '$event_ts' and invite.isactive = 0 and evnt.isactive = 0) AS invites_no,
        SUM(invite.friendID = $mID AND invite.decidedwhen != '0000-00-00 00:00:00' AND invite.maybe = 1 AND evnt.ends >= '$event_ts' and invite.isactive = 0 and evnt.isactive = 0) AS invites_maybe
FROM user_event_invite AS invite
JOIN user_event AS evnt ON evnt.eID = invite.eID

使用Codeigniters活动记录格式的东西。 EG

$this->db->select()
  ->from('user_event')
  ->... something?

标准的mysql语句工作得很好。但是,为了坚持制服,我想使用活动记录。这样做的任何方式?我找不到SUM()

的任何内容

2 个答案:

答案 0 :(得分:2)

(更多的评论而不是答案,因为我没有测试过;但它可能仍然有帮助)

活动记录有一个$this->db->select_sum();函数,(这是我不确定的一点)你可能可以使用方法链。所以这里有一些尝试。

第1阶段是将常见内容删除到WHERE中。如果将WHERE编入索引,它将加快速度。这将为您提供(同时修复了parenthasis - 您的SQL有错误)

SELECT
    SUM(invite.decidedwhen = '0000-00-00 00:00:00') AS invites_undecided,
    SUM(invite.decidedwhen != '0000-00-00 00:00:00' AND invite.yes = 1 ) AS invites_yes,
    SUM(invite.decidedwhen != '0000-00-00 00:00:00' AND invite.no = 1) AS invites_no,
    SUM(invite.decidedwhen != '0000-00-00 00:00:00' AND invite.maybe = 1) AS invites_maybe
FROM user_event_invite AS invite
   JOIN user_event AS evnt ON evnt.eID = invite.eID
WHERE invite.friendID = $mID  AND evnt.ends >= '$event_ts' and invite.isactive = 0 AND evnt.isactive = 0

另外,如果你可以简单地通过sayign接受,做出是,不,也许都是独家的,那么你可以剥离“决定”以及进一步简化。 (理想情况下,您只需要“COUNT”并减去其余部分,但不清楚如何在活动记录的一个查询中执行此操作。)

SELECT
    SUM(invite.decidedwhen = '0000-00-00 00:00:00') AS invitees_undecided,
    SUM(invite.yes = 1 ) AS invites_yes,
    SUM(invite.no = 1) AS invites_no,
    SUM(invite.maybe = 1) AS invites_maybe
FROM user_event_invite AS invite
   JOIN user_event AS evnt ON evnt.eID = invite.eID
WHERE invite.friendID = $mID  AND evnt.ends >= '$event_ts' AND invite.isactive = 0 AND evnt.isactive = 0

现在将其用于活动记录上的方法链:

$this->db->select_sum('invite.decidedwhen = '0000-00-00 00:00:00', 'invite_undecided')
  ->select_sum('yes', 'invite_yes')
  ->select_sum('no', 'invite_no')
  ->etc
  ->from('user_event_invite')
  ->join('user_event', 'user_event.eID=user_event_invite.eID)
  ->where('friendID', '$mID')
  ->where('user_event.ends >=', $event_ts)
  -> etc

答案 1 :(得分:0)

      Like that you can write query :
    ==================================
     $query="SELECT SUM(case when invite.friendID = " . $mID ." AND 
     invite.decidedwhen = '0000-00-00 00:00:00' AND evnt.ends >= '" . $event_ts . "'
     and invite.isactive = 0 and evnt.isactive = 0) AS invites_undecided,...       
     FROM user_event_invite AS invite
      left outer JOIN user_event AS evnt ON evnt.eID = invite.eID"