1

Im adding points to a db for when a user does somit on site. Alls I would like to do is get the points for the last 7 days and total them.

In my DB I have it saved like: PointsID, PointsUserID, PointsTotal, PointsDate

I guess I just need to figure out the latest date in the db, then minus 7 days, then get the values from between them. Once I have returned the values they will need to be summed so I can output one number.

Thanks, Bonxy

4
  • Do you need to get the points in the last 7 days from current date or the last 7 days that the user got any point? Commented Feb 27, 2012 at 1:19
  • Please show us what you have so far. Commented Feb 27, 2012 at 1:19
  • What is your date type? Are you using unix time or mysql date ? Commented Feb 27, 2012 at 1:22
  • Sorry guys. Im saving it in MySQL date/time. I want to be able to get the last 7 days from the current date using date("D");. Commented Feb 27, 2012 at 7:42

2 Answers 2

1
SELECT SUM(PointsTotal) FROM TableName WHERE PointsUserID = 'IdInQuestion' AND PointsDate >= Date_Sub(Now(), Interval 7 Day)

In the event that you are storing the date as a unix timestamp value (AKA a PHP date value) you would want to convert the one of the two values in the appropriate direction.

for instance you could convert the field to a MySQL datetime value by replacing PointsDate with FROM_UNIXTIME(PointsDate). You could also go the other way and convert the results of DATE_SUB() by wrapping it all in a UNIX_TIMESTAMP(). Both should have equal results.

Sign up to request clarification or add additional context in comments.

Comments

0

Something like

Select * From MyTable Where MyDate Between Date_Sub(Now(), Interval 7 Day) and Now()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.