MySQL select rows that are exactly 7 days old FROM TIMESTAMP - mysql

first of all, I know that my question is very similar to that one:
MySQL select rows from exactly 7 days ago
the difference is that my dates are stored in the database as a timestamp.
I know that I can use FROM_UNIXTIME to get the date from the timestamp, the thing is, in another answer I read that was very resource consuming (because the timestamp field has to be converted to date in all the records before comparing).
DATE(from_unixtime(timestamp)) = CURRENT_DATE()
Is there any optimized way to do this?

Turn it around: calculate the unix timestamp of the target date first and use that.
WHERE timestamp = UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)
MySQL should calculate that value once and use it all the time (needs testing though). If it doesn't, use a variable or code.

Related

Fetch mysql data of timstamp depending on time given

I have a column with timestamp, contain example value "2014-04-16 18:00:00","2014-04-17 18:00:00"....
Now, if I will call a page before "2014-04-17 12:00:00" I need this value-"2014-04-16 18:00:00"
And if I call my page after "2014-04-17 12:00:00" I need this value "2014-04-17 18:00:00".
I think my question is very complicated to understand, having complications in date & times, please check date & time properly.
I want to fetch this data from DB in mysql, The page I was saying is that where I'm going to add your mysql query.
Thanx in advance
Generalising what your asking for a bit the following will return dates from the previous day if it's before noon and dates from today if it's after noon:
SELECT date_column
FROM yourTable
WHERE DATE(DATE_SUB(NOW(), INTERVAL 12 HOUR)) = DATE(date_column);
Edit:
The WHERE clause First gets the current time (NOW()) and subtracts 12 hours. This wont affect the date unless the time is before 12. This means DATE_SUB(NOW(), INTERVAL 12 HOUR) gives us today if it's after noon and yesterday if it's before.
We then check if the date_column matches the date we've created (using the DATE function so that the time is ignored).
Adding some rows to the SELECT may help you see how these dates are built up.

What's the difference between a TimeStamp field and mysql curdate

I was working and my friend told me to use curdate() on mysql query to get the current date of the server... And I told him that I was using Time_Stamp field for date/time.
Now I start to think, is there a huge difference between this two ways ? One is better than the other? Or there is something that makes it a not good practice ? Also there is a now() that can be used too. I just wanted to understand how does it work or wich one is the best and why.
Short version:
NOW() = CONCAT(CURDATE(), ' ', CURTIME());
CURDATE() = DATE(NOW());
Some explanation:
NOW() gets both date (CURDATE()) and time (CURTIME()).
So if we do it the other way round, CURDATE() = DATE(NOW()).
Regarding timestamp, in MySQL Data Types we can see timestampis 3 bytes, while datetime is 8 bytes.

How to compare variable amount of days using UNIX timestamp (stored as int)

I need to pull a variable amount of days of data from a MySQL database that stores the date as a UNIX timestamp in an int column.
For example, if I need the last 5 days of data, what would my query be?
(All queries would start from current date and would go back x amount of days).
Timestamp is considered one of the Date and Time types and therefore any of the Date Time Functions can be used on it.
SELECT * FROM your_table
WHERE Ftimestamp_column > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 5 DAY));
I've never tried it but there's a MySQL function to convert unix timestamps into MySQL dates and then you can use DATE_SUB() or whatever. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime

MySQL date and hour issue

I have read that MySQL has some issues regarding date type. I want to get a date interval taking into account the hour. My question is can I do a between query, or make a column for the hour as well?
P.S:
select * from main where main.date between 01-06-11 07:59 and 01-06-11 15:59

Time difference in mysql

I want to get the records from mysql data base based on time.
I have lastquery_date column in my table every time update the date and time in that column.
I want to get time difference with current date is less than 12 mins
How can i get this query..
Please help me
see the link below and develop your own logic :)
TIMEDIFF
Your question is not entirely clear, but I think this is what you want:
SELECT *
FROM your_table
WHERE lastquery_date >= NOW() - INTERVAL 12 MINUTE