Welcome Guest, Not a member yet? Register   Sign In
Active Record Query
#1

[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.
#2

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

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

Thanks though!




Theme © iAndrew 2016 - Forum software by © MyBB