这里曾经有过Q

时间:2017-03-26 09:58:33

标签: mysql date between days

如标题中所述,此问题已被删除。

1 个答案:

答案 0 :(得分:4)

这将为您提供每个user_id的前7天记录

SELECT orders.* FROM orders
INNER JOIN (
    select user_id, min(created_on) as mindt from orders group by user_id
) t
ON orders.user_id = t.user_id AND orders.created_on <= DATE_ADD(t.mindt, INTERVAL 7 DAY)
ORDER BY user_id, created_on

对于每个用户的平均paid_amount,在前7天,请使用:

SELECT orders.user_id, avg(paid_amount) FROM orders
INNER JOIN (
    select user_id, min(created_on) as mindt from orders group by user_id
) t
ON orders.user_id = t.user_id AND orders.created_on <= DATE_ADD(t.mindt, INTERVAL 7 DAY)
group by orders.user_id