I have this query that works as desired, but it seems like there should be a better way to accomplish what I want:
SELECT `x`.*
FROM (
SELECT `m`.`id`,
`m`.`email`,
MAX(`s`.`end`) AS `max_end`
FROM `members` AS `m`
INNER JOIN `memberships` AS `s`
ON `m`.`id` = `s`.`member_id`
GROUP BY `m`.`id`,
`m`.`email`
) AS `x`
WHERE `x`.`max_end` = '2010-02-28 23:59:59'
I am looking for a member whose membership ends on a certain date. The memberships table has start and end columns contain the dates that the membership is active. I only want to look at the last ending membership period, hence the MAX() and GROUP BY in the sub-query.
This could be rewritten using
having: