Im trying to get the total amount of points a user has, as well as current month's points. When a user gets a point, it gets logged into the points table with a timestamp. Totals ignore the timestamp, while the current month's points looks for the points with the correct timestamp (from the first day of the month).
SELECT user_id, user_name, sum(tpoints.point_points) as total_points, sum(mpoints.point_points) as month_points
FROM users
LEFT JOIN points tpoints
ON users.user_id = tpoints.point_userid
LEFT JOIN points mpoints
ON (users.user_id = mpoints.point_userid AND mpoints.point_date > '$this_month')
WHERE user_id = 1
GROUP BY user_id
points table structure
CREATE TABLE IF NOT EXISTS `points` (
`point_userid` int(11) NOT NULL,
`point_points` int(11) NOT NULL,
`point_date` int(11) NOT NULL,
KEY `point_userid` (`point_userid`),
KEY `point_date` (`point_date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
This results in a very large number, thats equal to the sum of all points, multiplied by the number of rows that match the query.
I need to achieve this without the use of subqueries or multiple queries.