我在MySQL中有一个数据库,并且有一个名为user_daily_data_recorded
的表。其中有以下列
date_of_record
steps_walked
member_id
该表包含每个日期的数据,并定期填充。现在我想向用户/成员who have walked 15000 steps for at least two days per week over a month (Say in month February user1 has walked 15000 on Monday and Thursday would receive a congratulatory mail)
发送祝贺邮件。我该怎么办?如果我的问题不明确,请告诉我。
包含此脚本的文件每月只执行一次。因此,我需要在特定的一个月内找到每周15000步的两天。
答案 0 :(得分:0)
如果您想知道如何选择今天应该收到电子邮件的用户,这可能有所帮助:
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