dongwen5870 2013-04-17 11:08
浏览 100
已采纳

基于mysql时间戳的截止日期后的PHP和mysql查询

I've mysql DB, which stores timestamp (date + hour).

I'd like to use this data to create cutoff points, like week ago, month ago etc...

$result = $mysqli->query("SELECT 
u.user_id,
  ( SELECT sum(r.result_value)
    FROM logos_results r
    WHERE r.user_id = u.user_id
    AND r.result_date <= '$week' ) AS rsum,
u.user_name,
u.user_pic,
u.user_logos_level
FROM users u
ORDER BY rsum DESC
LIMIT 4");

My question is, how do I create week in php? Should I remove timestamp and use something without hours? Will this exact time decrease speed of query considerably? AND r.result_date <= '$week' this is the important part, is syntax correct?

  • 写回答

3条回答 默认 最新

  • douyao1994 2013-04-17 11:16
    关注

    Is there a reason why you won't use mysql ability to convert weeks to date or to select intervals by week?

    To me it seems better than implementing your week to date logic in php.

    Check this question.

    SELECT sum(r.result_value)
        FROM logos_results r
        WHERE r.user_id = u.user_id
        AND r.result_date < NOW() - INTERVAL $num_of_weeks WEEK
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?