Reputation: 36937
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()
Upvotes: 0
Views: 358
Reputation: 17710
(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)
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
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).
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
Now use that to method chain on the active record:
$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
Upvotes: 2
Reputation: 5588
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"
Upvotes: 0