CodeIgniter Forums
Active Record Query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Active Record Query (/showthread.php?tid=6142)



Active Record Query - El Forum - 02-15-2008

[eluser]Unknown[/eluser]
Hello, I'm new to CI and so far I'm loving it. I really want to use the Active record part of CI and for the most part I figured it out. This last query is givng me a little bit of trouble. Would realliy apreciate any help.

SQL
Code:
SELECT vb_user. * , vb_userfield. * , sum( vb_UserPoints.Points ) AS points
    FROM vb_user
    LEFT OUTER JOIN vb_userfield ON vb_userfield.userid = vb_user.userid
    LEFT OUTER JOIN vb_UserPoints ON vb_UserPoints.UserID = vb_user.userid
    WHERE vb_user.userid =1
    GROUP BY vb_user.userid

CI Code
Code:
$CI =& get_instance();
    
$CI->load->database();
    
$CI->db->select('user.*, userfield.*');
$CI->db->select_sum('UserPoints.Points AS points');
    
$CI->db->where('user.userid', '1');
$CI->db->join('userfield', 'userfield.userid = user.userid', 'left outer');
    
$CI->db->join('UserPoints', 'UserPoints.Userid = user.userid', 'left outer');

$results = $CI->db->get('user');

Error:
Quote:An Error Was Encountered
Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS points) AS vb_UserPoints.Points AS points FROM (`vb_user`) LEFT OUTER JOIN `v' at line 1

SELECT vb_user.*, vb_userfield.*, SUM(vb_UserPoints.Points AS points) AS vb_UserPoints.Points AS points FROM (`vb_user`) LEFT OUTER JOIN `vb_userfield` ON vb_userfield.userid = vb_user.userid LEFT OUTER JOIN `vb_UserPoints` ON vb_UserPoints.Userid = vb_user.userid WHERE vb_user.userid = '1'

I understand the error, but I'm not sure on how to get the SUM function to work.


Active Record Query - El Forum - 02-16-2008

[eluser]Seppo[/eluser]
How about this?
Code:
$CI->db->select_sum('UserPoints.Points', 'points');



Active Record Query - El Forum - 02-16-2008

[eluser]Unknown[/eluser]
Yea I figured that out last night. Missed that part in the docs.

Thanks though!