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
Related
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 have a table with a few records and for each of these records I've also added a UNIX_TIMESTAMP. Now, I also have a search engine for those records in which I can choose a date using a jQuery datapicker. My question is how do I make the request so that to select all timestamps from the database for a certain date.
With an index on your timestamp column you will get a faster result with:
SELECT *
FROM table_name
WHERE time_stamp_column_name >= :date_picked
AND time_stamp_column_name < :date_picked + INTERVAL 1 DAY
Where :date_picked is your bound-in picked date as a string in the format 'YYYY-MM-DD'.
You can use from_unixtime to convert it into a date
select *
from
table
where
date(from_unixtime(your_timestamp_col)) = '2014-10-01'
use unix timestamp
you can also see this
Hi there please help me if you can. Here is my senario:
I have a MySQL database with a column that holds a date in the form of a varchar. The format of the date is the following 29/05/2014 (i.e. d/m/Y).
I'm trying to compare the value of this column with todays date and return any rows where the date is earlier than todays date.
I'm using a php variable to store todays as follows:
$date = date("d/m/Y");
Here is my SQL query:
SELECT * FROM patients WHERE last_seen < '$date'
What gets returned
So what is returned is very unusual (to me). All records where the last_seen "day" is less than todays "day". It seems to be overlooking the month and year. So in other words if I last_seen = "30/05/2014" and todays date is "29/05/2014" this record is still returned.
Does anyone have any ideas what I might be doing wrong here?
Thanks
You really, really shouldn't store dates in a varchar field - use date or datetime or timestamp data type.
That said, sometimes you don't have control over the database and you have to deal with somebody else's bad design decision. In this case, to compare dates, convert the varchar strings to dates and compare them that way. So, in your case, you can have something like this:
$date = date("d/m/Y");
and then
SELECT * FROM patients WHERE str_to_date('last_seen', '%d/%m/%Y') < str_to_date('$date', '%d/%m/%Y')
or simpler
SELECT * FROM patients WHERE date(last_seen) < current_date
This way you are actually comparing dates and not strings containing dates. Naturally, this assumes that all dates are stored in the same format.
EDIT: I just tested the last option - and, apparently, date('30/05/2014') returns NULL on my system (mysql 5.5 on linux), hence I suggest the best way is
SELECT * FROM patients WHERE str_to_date('last_seen', '%d/%m/%Y') < current_date
You need to store your date as DATE or DATETIME in your database.
Then you can use:
SELECT * FROM patients WHERE DATE(last_seen) < CURRENT_DATE
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()
I have a UNIX timestamp in a table like 1321521158 and want to get the details from a table for the particular date, like:
$mydate="11/11/2011";
SELECT notes FROM table WHERE `addeddate`=$mydate
How do I get it?
you can use the mysql date function like:
WHERE date(addedate) = date(mydate)
or you can calculate the starting and ending timestamp for the given day you want to know and then do
WHERE addeddate >= mystarttimestamp AND addeddate <= myendtimestamp
try
WHERE $mydate = FROM_UNIXTIME('dateColumn')'%Y/%D/%M');
MySQL Date Time Functions