Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8775289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:46:05+00:00 2026-06-13T18:46:05+00:00

Im trying to figure out the best way to convert this mysql query SELECT

  • 0

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()

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:46:06+00:00Added an answer on June 13, 2026 at 6:46 pm

    (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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to figure out the best way to do this: Should I do something
I'm trying to figure out the best way to do this, but I'm getting
I am trying to figure out the best way to accomplish this given the
I'm trying to figure out the best way to do this...I am using JQuery
Trying to figure out the best way to do this (And without crossing any
I'm trying to figure out the best way to achieve this: I need to
I'm trying to figure out the best way to convert a string containing a
Trying to figure out the best way to accomplish this. I have inhereted a
I'm trying to figure out the best way to do something like this. Basically
I am trying to figure out the best way to parse this comma delimited

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.