0

我试图找出转换这个 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 活动记录格式的东西。例如

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

标准的 mysql 语句工作得很好。但是以坚持统一的名义,我想使用活动记录。有什么办法吗?我找不到任何东西SUM()

4

2 回答 2

2

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

有一个$this->db->select_sum();用于活动记录的功能,并且(这是我不确定的一点)您可能可以使用方法链。所以这里有一些东西可以尝试。

第 1 阶段是将常见的东西剥离到 WHERE 中。如果 WHERE 被索引,它会加快速度。这会给你(括号也固定 - 你的 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 接受来简化,使yes,no,也许都是排他性的,那么您也可以去掉“decidedwhen”以进一步简化。(理想情况下,您只需使用“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
于 2012-11-02T04:29:11.980 回答
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"
于 2012-11-02T04:24:25.227 回答