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
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.
Something like
Select * From MyTable Where MyDate Between Date_Sub(Now(), Interval 7 Day) and Now()
Related
I'm trying to do a SELECT * FROM but only items that are less than 30 days old. Here is my select code:
SELECT * FROM `{$table_name33}` WHERE `type`='wpst-requiredinfo' ORDER BY `foreignkey` ASC;
However, my problem is that I can't figure out how to add WHERE AND last_updated is less than 30 days.
I'm not exactly sure how to write the query, but the date is showing up like this: 1428412603 in the table column, it doesn't look much like a date to me. I don't know where to start.
Try this where clause:
WHERE `type`='wpst-requiredinfo' and
last_updated >= date_sub(now(), interval 30 day)
EDIT:
Your date seems to be in Unix time format.
WHERE `type`='wpst-requiredinfo' and
last_updated >= unixtime_timestamp() - 30*24*60*60
Note: this puts all the functions on the current time. In particular, it does not use FROM_UNIXTIME(last_updated). This ensures that an index can be used for this part of the query. The best index would be on (type, last_updated).
In my MySQL database there is a dateofjoin (int) field in the user table. dateofjoin is inserted like following format:
$dateofjoin=mktime(0,0,0,02,22,2015);
Now I want list of dates that lie between two dates by select query. where two dates are automatically change in mysql query. I have written like this
SELECT *
FROM user
WHERE
(MONTH(STR_TO_DATE(dateofjoin, '%m/%d/%Y')) = MONTH( CURDATE() + INTERVAL 15 DAY)
AND DAY(STR_TO_DATE(dateofjoin,'%m/%d/%Y')) = DAY(CURDATE() + INTERVAL 15 DAY))
Assuming dateofjoin is date or datetime,
SELECT * FROM user
WHERE DATE(dateofjoin) = CURDATE()+INTERVAL 15 days
or something like that. (You haven't specified how you are getting your 2 boundary dates, so I'm just using your 30 days from now.)
However, the fact that you are using STR_TO_DATE() on dateofjoin makes me think maybe you have it stored as a varchar or something? (Why?!)
I've a table called graphs and it has my fields like:
id, name, channel, created.. etc. created field is DATETIME.
Now, I want to retrieve data with one minute time interval. That is, date difference with current time and created field will be one minute.
Here created field date format is: Y-m-d h:i:s.
What is the solution? Please help.
use this query
SELECT * FROM tbl_name WHERE DATE_SUB(NOW(),INTERVAL 1 MINUTE) <= created;
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
One possible query might be:
SELECT * FROM graphs WHERE (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(`created`) <= 60)
Check UNIX_TIMESTAMP
I am trying to select all records in a table which have a date between the current date and 1 month ahead.
The date is stored like this DD-MM-YYYY
And the query I have tried:
SELECT * from tablename WHERE renewalDate BETWEEN DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')) AND DATE_ADD(DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')), INTERVAL 1 MONTH)
But this does not return the correct results.
Is the date stored in an actual date or datetime field? If it's in a char/varchar field, you won't be able to use the BETWEEN syntax, as mysql will just treat them as fixed strings.
I am currently developing a sports website where one of the pages with be forthcoming fixtures in which the user will be able to what team and where the team are playing their next match.
I have a database with the following fields...
ID
TEAM NUMBER
OPPOSITION
VENUE
DATE
MEET TIME
MATCH TYPE
So a row of data pulled from the DB and print_r'd may look like this
ID=>[1] TEAM NUMBER=>[1] OPPOSITION=>[YORKSHIRE] VENUE=>[HOME] DATE=>[2009/4/25] MEET TIME=>[13.00] MATCH TYPE=>[CUP]
My problem is i cannot work out how to show the next match dependent on what the current date is, so for example for now I want the site to show all the games that will happen over the weeken of the 25th April 2009 and then once that has gone the fixtures for the next weekend.
Hope this makes sense and some one give me an idea of how to tackle this.
select * from my_events where date between now() and date_add(now(), interval 7 day);
Should do it I think.
Instead of relying entirely on MySQL, you can also use PHP's strtotime() function:
$query = "select * from my_events where date between now() and ".
date("Y-m-d", strtotime("+1 week"));
For MySQL check out the Date and Time functions. You can use a combination of CURDATE() and ADDDATE() to achieve what you need.
Your description is very vage but try something like this:
SELECT all_fields_you_need
FROM table_name
WHERE `DATE` > CURDATE() AND `DATE` <= DATE_ADD(CURDATE(), INTERVAL 7 DAY)
ORDER BY `DATE` ASC
(not tested, just written as it came into my mind...)
Load it all into an array and display the data
you can get the system date (in Oracle using sysdate) and then add to it, so look for all records where DATE = sysdate + 7. You may have to play with this a little, formatting the date so that sysdate + 7 returns a date without the time, but that is basically what you need.
EDIT:
If you want the event between now and a week from now (if games are only on the weekend, then this will return next weekend's games) do
DATE > sysdate AND DATE <= sysdate + 7
To get the next match for team xxx
SELECT *
FROM myTable
WHERE TEAM NUMBER = xxx
AND DATE = ( SELECT MIN(DATE)
FROM myTable
WHERE TEAM NUMBER = xxx
AND DATE > NOW() )
I suspect this is what you really want, if matches only take place at weekends (which seems to be an assumption from your question).
Today + 7 days is not the same as next weekend unless today happens to be the same day of the week as the match.