Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql: get record count between two date-time

I am stuck with a problem in MySQL. I want to get the count of records between two date-time entries.
For example:
I have a column in my table named 'created' having the datetime data type.

I want to count records which were created date-time between "TODAY'S 4:30 AM" and "CURRENT DATE TIME".

I tried some of MySQL's functions but still no luck with it.

Can you please help me with this? thanks.

like image 762
gautamlakum Avatar asked Apr 26 '11 06:04

gautamlakum


People also ask

How do I count days between two dates in MySQL?

To count the difference between dates in MySQL, use the DATEDIFF(enddate, startdate) function. The difference between startdate and enddate is expressed in days. In this case, the enddate is arrival and the startdate is departure .

How can I get data between two timestamps in SQL?

As stated above, the format of date and time in our table shall be yyyy:mm: dd hh:mm: ss which is implied by DATETIME2. The time is in a 24-hour format. Syntax: SELECT * FROM TABLE_NAME WHERE DATE_TIME_COLUMN BETWEEN 'STARTING_DATE_TIME' AND 'ENDING_DATE_TIME';

How do I select a date range in MySQL?

If you need to select rows from a MySQL database' table in a date range, you need to use a command like this: SELECT * FROM table WHERE date_column >= '2014-01-01' AND date_column <= '2015-01-01'; Of course you need to change: table.

How can I get the difference between two dates in a month in MySQL?

Month-difference between any given two dates: Have a look at the TIMESTAMPDIFF() function in MySQL. What this allows you to do is pass in two TIMESTAMP or DATETIME values (or even DATE as MySQL will auto-convert) as well as the unit of time you want to base your difference on.


1 Answers

May be with:

SELECT count(*) FROM `table`  where      created_at>='2011-03-17 06:42:10' and created_at<='2011-03-17 07:42:50'; 

or use between:

SELECT count(*) FROM `table`  where      created_at between '2011-03-17 06:42:10' and '2011-03-17 07:42:50'; 

You can change the datetime as per your need. May be use curdate() or now() to get the desired dates.

like image 138
Harry Joy Avatar answered Sep 26 '22 04:09

Harry Joy