Results 1 to 2 of 2
Jan 17, 2011, 06:24 #1
- Join Date
- Jan 2011
- 0 Post(s)
- 0 Thread(s)
getting the SUM of 2 fields and subtracting
Users on my site can aqquire points for certain actions. My table is:
member_id | points_count | points_type | points_for | date
points_count are the points that are credited/debited
points_type is either a credit (1) or debit (2).
I had originally just added and subtracted the points in my member table, but now id like to keep track of what the points were credited for.
SUM(credits.points_count) as points_credits,
SUM(debits.points_count) as points_debits
FROM loyalty_balance as credits
JOIN loyalty_balance as debits ON debits.member_id = '".$_SESSION['kt_login_id']."' AND debits.points_type = '2'
WHERE credits.points_type = '1' AND credits.member_id = ".$_SESSION['kt_login_id']."
$loyalty_points = $row['points_credits']-$row['points_debits'];
SUM(credits)-SUM(debits) as loyalty_points
Jan 17, 2011, 06:35 #2
- Join Date
- Jul 2002
- Toronto, Canada
- 52 Post(s)
- 2 Thread(s)
SELECT points_credits , points_debits , points_credits - points_debits AS loyalty_points FROM ( SELECT SUM( CASE WHEN points_type = 1 THEN points_count ELSE NULL END ) AS points_credits , SUM( CASE WHEN points_type = 2 THEN points_count ELSE NULL END ) AS points_debits FROM loyalty_balance WHERE member_id = ".$_SESSION['kt_login_id'] ) AS d