Im trying to figure out the best way to convert this mysql query
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
to something that uses Codeigniters active record formatting. EG
$this->db->select()
->from('user_event')
->... something?
The standard mysql statement works just fine. But in the name of sticking uniform to everything I’d like to use the active record. Any way of doing this? I can’t find anything for SUM()
(More of a comment than an answer as I’ve not tested it; but it may still help)
There is a
$this->db->select_sum();function for the active record, and (this is the bit I’m not sure about) you probably can method chain. So here’s something to try.Stage 1 is to strip out the common stuff into a WHERE. If the WHERE is indexed, it’ll speed things up. This will give you (with the parenthasis fixed too – your SQL has an error)
Also, if you can simplify by sayign accepting, making yes, no, maybe are all exclusive then you can strip off the “decidedwhen” as well to simplify further. (Ideally you’d just to a “COUNT” and subtract the rest, but not clear on how to do that in one query on the active record).
Now use that to method chain on the active record: