Fetch mysql data of timstamp depending on time given - mysql

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.

Related

SQL how can i get previous minute?

I have a data table whose structure looks like this.
date time order_id action quantity
How can I query to know how many entries are made to this table previous minute?
Say suppose the time now is 14:46 I want to know how many row entries were made to this table at 14:45. how can I do that?
The problem I am facing now is that I don't know how can I get last minute's time stamp. CURRENT_TIMESTAMP() is giving me correct current time. But I tried CURRENT_TIMESTAMP()-1 which gives some decimal number.
you can use DATE_SUB(), specifying a date and an INTERVAL as documented for DATE_ADD(). Example:
SELECT DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 MINUTE);
You can use my query to find the last minute :
SELECT DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 MINUTE);

mySQL gather data between hours of the day passing through midnight with date increment

I am trying to get data from a database between 8PM (say, today) and 2AM tomorrow.
I have been using clauses such as where hour(date_field)>=20 and hour(date_field) <23 to obtain data in the same day.
Here the date_field is datetime
All I want is to be able to tell SQL to get data after 8PM today, increment the datefield and then get data till 2AM tomorrow.
Any help will be appreciated.
The normal pattern for retrieving rows based on a datetime range is perform comparisons on the bare column, comparing the column value to constants derived from expressions.
To get rows for a single contiguous range, 8PM today to 2AM tomorrow, for example:
WHERE t.date_column >= DATE(NOW()) + INTERVAL 20 HOUR
AND t.date_column < DATE(NOW()) + INTERVAL 26 HOUR
To unpack that a little bit: NOW() returns current datetime, the DATE() function truncates the time portion to midnight, then we add back in enough hours to get '8PM today', or enough hours to get '2AM tomorrow'.
If you are meaning to retrieve multiple "8PM to 2AM" periods, for a whole series of days.
First, you'd want an upper and lower bound of the date_column to be retrieved (unless you want every possible date)
WHERE t.date_column >= '2014-08-01 20:00:00'
AND t.date_column < '2014-10-02 02:00:00'
From that, we need to filter out all of the rows that aren't between 8PM and 2AM. One convenient way to do that would be to "subtract" two hours from the datetime col, and check for hour >= 6PM.
AND HOUR(t.date_column + INTERVAL -2 HOUR) >= 18
Note that the expression involving date_column will need to be evaluated for EVERY row in the table, unless there are some other predicates that filter rows out. With a suitable index available, MySQL can use an index range scan operation for predicates of the form date_column >= const and date_column < const. (It can't do that when the column is wrapped in a function or expression.)

Selecting PHP data by date for the last 7 days from mysql database with a different date layout

I have a transactions table in my database where the date is stored in the date field like this: 2014-08-30 02:22:35.
I'm making basic analytics and need to be able to display all transactions for each day for the last 7 days but am a little confused as to how I can achieve this when there is a timestamp along with the date stored in the same field.
Any suggestions would be greatly appreciated.
You can get the last seven days with:
where `date` >= CURDATE() - interval 7 day
This will go back seven days, ignoring the time field.
I'm not sure what you mean by "display all transactions for each day for the past 7 days". You can extract just the date for the field using date (so, date(date)) and use the value for filtering, aggregation, or sorting.

MySQL select rows that are exactly 7 days old FROM TIMESTAMP

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.

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