如果您想知道如何选择今天应该收到电子邮件的用户,这可能会有所帮助:
SELECT member_id
FROM user_daily_data_recorded uddr
# Selects all users which created a record today.
JOIN (SELECT DISTINCT ( member_id ) AS id
FROM user_daily_data_recorded
WHERE date_of_record = Curdate()) AS todays_users
ON todays_users.id = uddr.id
# Selects all users which where below 15000 steps yesterday.
JOIN (SELECT DISTINCT ( member_id ) AS id
FROM user_daily_data_recorded
WHERE Sum(steps_walked) < 15000
AND date_of_record < Curdate()
GROUP BY member_id) AS yesterday_below_mark_users
ON yesterday_below_mark_users.id = uddr.id
# Selects all members which are over 15000 steps today.
WHERE Sum(uddr.steps_walked) >= 15000
GROUP BY member_id
我没有测试它,但可能这些步骤可以帮助你:)
编辑:按周超过标记:
SELECT DISTINCT( member_id )
FROM user_daily_data_recorded uddr
JOIN (SELECT member_id AS id,
Count(date_of_record) AS dates_over_mark
FROM user_daily_data_recorded
WHERE Month(date_of_record) = Month(Curdate())
AND steps_walked >= 15000
GROUP BY Week(date_of_record)
HAVING dates_over_mark >= 2) AS very_active_members
ON very_active_members.id = uddr.members_id