0

我在 MySQL 中有一个数据库,并且有一个名为user_daily_data_recorded. 有以下列

  1. date_of_record
  2. steps_walked
  3. member_id

该表包含每个日期的数据,并定期填充。现在我想向 user/members 发送祝贺邮件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 步的两天。

4

1 回答 1

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  
于 2013-08-26T10:06:23.993 回答