I am trying to fetch rows only from specific date (like today, yesterday or 2 days ago) in mySQL. I have a column named "date" in my rows. (which includes dates like 1365053426).
$result=mysql_query("SELECT count(*) as total from track WHERE `date` >= CURRENT_DATE
AND `date` < CURRENT_DATE + INTERVAL 1 DAY");
I have tried this query, but it returns "0". What is the correct way to do that ?
how about using BETWEEN?
SELECT COUNT(*) as TotalCount
FROM Track
WHERE Date BETWEEN CURDATE() + INTERVAL -2 DAY AND CURDATE()
How about using datediff() function?
SELECT count(*) as total from track WHERE datediff(now(),date)=interval day
note: interval day could be declare from 0 -> up depends on what previous date you want to show
Related
i would like to count the date from the database where the month is now.
my code goes like
$sql="SELECT count(date) FROM tbl_upcoming where
date=MONTH(NOW())";
if ($results=mysqli_query($conn,$sql)){
$datecount=mysqli_num_rows($results);
}
for example: '2017-3-20', '2017-3-4', '2017-1-3', it will return 2.
Try something like this:
SELECT date, count(1) FROM tbl_upcoming where
MONTH(date)=MONTH(NOW()) and YEAR(date) = YEAR(NOW()) GROUP BY date
You were almost there:
SELECT count(*) FROM tbl_upcoming WHERE MONTH(the_date)=MONTH(NOW())";
The approach of MONTH(NOW()) is good, this will return you the current month.
However you were comparing the current month with a whole date, which can't work. You also have to apply the MONTH() function to your date to compare months with months
You should check like this,
month can be from past year or next year also,
select count(*) as cnt from tbl_upcoming
where MONTH(date) = MONTH(CURRENT_DATE) and YEAR(date) = YEAR(CURRENT_DATE);
I hope this will help.
You want to know how many rows are in the current month?
SELECT COUNT(*)
FROM tbl_upcoming
WHERE `date` >= CURDATE() - INTERVAL DAY(CURDATE())-1 DAY;
Notes:
In most situations, say simply COUNT(*).
CURDATE() is midnight this morning, hence easier to deal with for your query.
CURDATE() - INTERVAL DAY(CURDATE())-1 DAY gives you the DATE of the first of this month.
The formulation avoids all hiccups with various datatypes: DATE, DATETIME, DATETIME(6), TIMESTAMP, etc.
The next note handles leapdays, end of year, also.
If you have dates reaching into future months, then
add
AND `date` < CURDATE() - INTERVAL DAY(CURDATE())-1 DAY
+ INTERVAL 1 MONTH;
Need help here, having an mysql table called APPROVAL, there having an id,dateandtime and level, i need a query that selects the id alone with the following condition.
Taking date alone from database and comparing it with current system date, if the days exceeds above 30 and below 60 and also level = 5.
How can I write a query for this.
Thanks in advance.
MySQL has good date arithmetic. For example, the expression
CURDATE() + INTERVAL 30 DAY
gives a datetime value denoting midnight 30 days hence. Similarly
CURDATE() + INTERVAL 61 DAY
yields midnight on the 61st day.
So a query of the form
SELECT ID
FROM APPROVAL
WHERE Level = 5
AND `DateTime` >= CURDATE() + INTERVAL 30 DAY
AND `DateTime` < CURDATE() + INTERVAL 61 DAY
will yield what you want. Notice the use of >= for the beginning of the range of days, and the use of < and an extra day for the end of the range. We do that because we want all items from the 60th day, and none from the 61st day.
A compound index on (Level, DateTime) will make this query very efficient to satisfy.
Notice that an expression like
DATE(`DateTime`) <= CURDATE() + INTERVAL 60 DAY /* slow! */
will also yield correct results, but the presence of the the DATE() function call on the column to be searched makes it unsargeable. That is, it makes MySQL unable to use an index to satisfy the search.
Ok so use this query to retrieve all the IDs that match level 5 and date diff between 30 and 60 compared to the current date.
SELECT id
FROM APPROVAL
WHERE level = 5 && DATEDIFF(NOW(), dateandtime) BETWEEN 30 AND 60
I'd suggest you to order them dy date DESC too.
Hope that helps
I hope, I understood your problem correctly.
select `ID`
from APPROVAL
where `Level` = 5
and ( DATE(`DateTime`) > curdate() + interval 30 day
and DATE(`DateTime`) < curdate() + interval 60 day )
order by `ID` asc;
Where DATE() gets the date from a datetime and CURDATE() is the current system date. With interval you can manipulate a date expression whitout having to worry about its limits.
I want to make a date interval in a MySQL query. The date is extracted dynamically from an HTML Form and I want to search if something is between 2 days after and after a fixed date. I've made an example but it's not working well.
SELECT * FROM `sessions` WHERE `start_date` = '2014-05-12'
or `start_date` between DATE_SUB(start_date, INTERVAL 1 DAY)
and DATE_ADD(start_date, INTERVAL 1 DAY)
Any other propositions ?
If I'm not wrong every time this query is resulting all rows from the sessions table. Let's take a look at this particular part of the query,
start_date between DATE_SUB(start_date, INTERVAL 1 DAY)
and DATE_ADD(start_date, INTERVAL 1 DAY),
So if the start_date value of a row is 22-07-2014 the query translates to,
start_date between 21-07-2014 and 23-07-2014)
So every row satisfies the condition as every date x is between x - 1th day and x + 1th day.
So if you want to execute the query to find a date between a certain interval, try
start_date between DATE_SUB('$start_date', INTERVAL 1 DAY)
and DATE_ADD('$start_date', INTERVAL 1 DAY)
instead. Note that '$start_date' is a php variable that is the date you are comparing the row with.
I have date and time column in my table. I want to select all objects which has date and time larger than current time plus one hour:
I have tried the following:
SELECT * FROM mytable WHERE date >= CURDATE() AND time >= TIME(NOW()+INTERVAL 1 hour)
However this is logically not correct. If date is tomorrow and time is less than current HH:MM it will not select that record.
How can I use my date and time to compare with current datetime?
I am using php 5.2 with mysql
First of all, I recommend using a DATETIME column to make this filtering more efficent. However, this works if you cannot change:
SELECT * FROM mytable
WHERE (date = CURDATE() AND time >= TIME(NOW() + INTERVAL 1 hour))
OR date > CURDATE()
You can split your condition into two conditions...
When the date is equal to today and more than an hour ahead of the current time:
date == CURDATE() AND time >= TIME(NOW()+INTERVAL 1 hour)
When the date is greater than today:
date > CURDATE()
...and chain them together using OR and parentheses:
SELECT * FROM mytable WHERE (date == CURDATE() AND time >= TIME(NOW()+INTERVAL 1 hour)) OR date > CURDATE()
First check whether the date is today and if time is greater then current time. The second condition is to check whether the date is greater than current date. Because if the date is greater then current date then automatically it's time is ahead.
SELECT * FROM mytable
WHERE (date = CURDATE() AND time >= TIME(NOW() + INTERVAL 1 hour))
OR date > CURDATE()
SELECT * FROM mytable WHERE date >= CURDATE() AND time >= TIME(HOUR(NOW()) + 1);
or maybe just
SELECT * FROM mytable WHERE date >= CURDATE() AND time >= HOUR(NOW()) + 1;
As indicated by #Pekka, all the answers posted so far will give the wrong answer between 11pm and midnight. I don't work with MySQL, but I can look stuff up. A better approach seems to be the timestamp() function.
This reference states:
With two arguments, it adds the time expression expr2 to the date
or datetime expression expr1 and returns the result as a datetime value.
Why not use timestamp() with two arguments?
You can do this.
SELECT
*
FROM
mytable
WHERE
CONCAT( date, " ", time ) > (NOW() + INTERVAL 1 HOUR );
Personally I suggest that you get rid of the separate fields for date and time and create a consolidated 'DATETIME' field. I also suggest adding an INDEX to that field.
That would make your queries better and faster. :)
I am trying to filter the count of Rows found, per hour, over the past 24 hours.
I am using the following SQL query and 'landtime' is a DateTime field.
SELECT HOUR(landtime) as hour, COUNT(*) as total FROM ord_log WHERE landtime > DATE_SUB(NOW(), INTERVAL 24 HOUR) GROUP BY HOUR(landtime) ORDER BY HOUR(landtime)
Sample data set > http://pastebin.com/0rYBnePG
The results looked as expected, until I looked at the data it was using.
It was pulling dates from days/weeks before todays date when I only want a count for the past 24 hours.
Any help appreciated!
SELECT * FROM table WHERE day(data) = EXTRACT(day FROM (NOW() - INTERVAL 1 day))
No need to use a function, just use simple math:
SELECT HOUR(landtime) as hour, COUNT(*) as total FROM ord_log WHERE landtime > (CURRENT_TIMESTAMP - INTERVAL 1 DAY) GROUP BY HOUR(landtime) ORDER BY HOUR(landtime)