I have this query that looks up results from a database for the last twenty minutes, now i know how to look up in hours, days, etc, but is it possible to look up only as far back as midnight of that day. so when ever the query is run and what ever time it only looks back as far as midnight?
SELECT * FROM ip_stats WHERE date >= ( NOW() - INTERVAL 20 MINUTE ) and ip='$ip'
This is my code but is there away in which i can replace the interval for a specific time.
Any help would be appreciated.
Looking back to midnight of the current day is the same as looking at the current date with no time component. You can therefore use DATE() to truncate the datetime column date to only the date portion, and compare it to CURDATE().
SELECT * FROM ip_stats WHERE DATE(date) = CURDATE() and ip='$ip'
SELECT * FROM ip_stats WHERE date >= ( NOW() - INTERVAL 20 MINUTE ) AND date >= CURDATE() and ip='$ip'
Just make the column date be bigger than the curdate (which is the starting of this day).
SELECT * FROM ip_stats
WHERE date >= ( NOW() - INTERVAL 20 MINUTE )
and date > CURDATE()
and ip='$ip'
You can use CURDATE() to get rows since midnight of the current day:
SELECT * FROM ip_stats WHERE date >= CURDATE() and ip='$ip'
SELECT *
FROM ip_stats
WHERE date >= GREATEST( NOW() - INTERVAL 20 MINUTE, CURRENT_DATE )
AND ip = '$ip'
Related
I have a varchar field contain a timestamp value, i want to get the tomorrow records.
This is my code as you can see:
$tomorrow = strtotime("1+ day");
SELECT * FROM tasks WHERE task_accomplish_date > $tomorrow
but the query is incorrect
thanks for the help in advance
Should be like this :
$tomorrow = strtotime("+1 day");
/* this will select all record before tomorrow*/
SELECT * FROM tasks WHERE task_accomplish_date < $tomorrow;
/* this will select all record after tomorrow*/
SELECT * FROM tasks WHERE task_accomplish_date > $tomorrow;
http://php.net/manual/en/function.strtotime.php
SELECT * FROM tasks WHERE task_accomplish_date > NOW() + INTERVAL 1 DAY
You should be able to do this all in the MySQL query
SELECT *
FROM `tasks`
WHERE DATE(`task_accomplish_date`) = DATE(NOW() + INTERVAL 1 DAY)
That converts your task_accomplish_date into a date value and looks for records where that date is equal to today + 1 day (tomorrow). That does not give you records from 2 days from today or beyond, just tomorrow. If you want tomorrow and all records beyond tomorrow you would use
SELECT *
FROM `tasks`
WHERE DATE(`task_accomplish_date`) >= DATE(NOW() + INTERVAL 1 DAY)
If you are storing a timestamp, these only care about the date part of the timestamp, not about the time part of the timestamp. If you care about the time part as well you can remove DATE() from both sides of the = in the WHERE clause
I am querying records from the last calendar month. As it is February, it should return all the records that were added on January this year.
My Query:
`SELECT * FROM table_name WHERE date >=
DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1
DAY) AND date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1
MONTH)), INTERVAL 0 DAY) AND campaign = '$campaign' ORDER BY date
ASC`
It returns some records but skips the first 9 days. It starts showing records from 10th of the previous month. What am I missing here?
check your date field type and make sure you have not mistaken it with varchar.
SELECT * FROM table_name WHERE
(MONTH(date) = (MONTH(NOW()) - 1) AND YEAR(date) = YEAR(NOW()))
OR
(MONTH(date) = 12 AND MONTH(NOW())=1 AND YEAR(date) = (YEAR(NOW()) - 1) AND campaign = '$campaign' ORDER BY date
ASC`
Try To Get Data in step by step like,
First, you should try below query.
SELECT Date, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY) AS StartDate, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY) AS EndDate FROM MyTable
Second, If First Step give right date then get your data by directly writing your date rather than DATE_ADD function in where clause.
Third, If These will Give you write DATA then try to fetch data using DATE_ADD function.
Replay If you will get solution.
SELECT * FROM table_name WHERE date between DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 MONTH) AND DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 DAY) AND campaign = '$campaign' ORDER BY date ASC
I would like to know how can I get the leading zero's here. I already tried %H AND %h but still not working.Any help would be appreciable.Thank you
SELECT * FROM events WHERE eventDate = DATE_FORMAT(CURDATE(), '%d-%m-%Y') AND time BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 2 HOUR),'%H:%i') AND DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 10 MINUTE),'%H:%i')
UPDATE:
It just doesn't work between 00:00 and 02:00.. Do someone has any ideia why it's happening??
As you said your problem only exists when time between 00:00 and 02:00, it caused by this code:
DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 2 HOUR),'%H:%i')
Let's break your code into few parts:
select date_format(curdate(), '%d-%m-%Y') as date_only,
DATE_FORMAT(DATE_SUB(now(), INTERVAL 2 HOUR),'%H:%i') as min2h,
DATE_FORMAT(DATE_ADD(now(), INTERVAL 10 MINUTE),'%H:%i') as add10m
# assume now = 2016/10/28 01:00
# the date_only will return 2016/10/28
# the min2h will return 23:00
# the add10m will return 01:10
back to your query, you will have a query like this:
SELECT * FROM events WHERE eventDate = '20161028' AND time BETWEEN '23:00' AND '01:10'
That's why you can't get the result which the statement is not correct. You should convert your eventDate and time to datetime first, so that you can compare the datetime correctly when minus 2 hours is yesterday or add 10 minutes is tomorrow
select * from events
where str_to_date(concat(`eventDate`, " ", `time`), '%d-%m-%Y %H:%i') # convert o datetime, depends on your format
between date_sub(#report_date, interval 2 hour)
and date_add(#report_date, interval 10 minute)
Fiddle
I have to filter the results of an SQL starting at the current day and displaying all records until the current day + 10 days. So if it's the 22th of December I should display all records starting that day and that aren't after the the 1st of January. How can I do this? I tried a simple query like the one below but it seems to only display the records until the last day of the current month and then instead of showing the records of the next year goes back to the first month of the current year.
SELECT *
FROM mytable
WHERE DAY(mydatefield) >= DAY(CURRENT_TIMESTAMP)
AND mydatefield <= DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 10 DAY)
EDIT 1
I'm using Symfony 2 + Doctrine 2 querybuilder so the query must be compatible with them
EDIT 2
I solved the Doctrine 2 problem by using the query suggested by eggyal with this PHP code
$queryBuilder->where($queryBuilder->expr()->gte('mydatefield', 'CURRENT_DATE()'))->andWhere($queryBuilder->expr()->lte('mydatefield', 'DATE_ADD(CURRENT_DATE(), 10, \'DAY\')'));
The problem lies in your erroneous use of MySQL's DAY() function, which returns the day of the month. You should use instead DATE(); you can also simplify with the BETWEEN ... AND ... comparison operator:
SELECT *
FROM mytable
WHERE DATE(mydatefield) BETWEEN CURDATE() AND CURDATE() + INTERVAL 10 DAY
Note that, in order to benefit from index optimisation, you could instead do:
SELECT *
FROM mytable
WHERE mydatefield >= CURDATE() AND mydatefield < CURDATE() + INTERVAL 11 DAY
You should be able to use this:
SELECT *
FROM mytable
WHERE date(mydatefield) >= date(CURRENT_TIMESTAMP)
AND date(mydatefield) <= date(DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 10 DAY))
See SQL Fiddle with Demo
try this
SELECT *
FROM mytable
WHERE DATE( mydatefield ) BETWEEN CURRENT_DATE AND CURRENT_DATE + INTERVAL 10 DAY
use PHP and MySQL and want to use SELECT statement which date_post(datetime variable) start at the last date of last month and to date the first day of next month, help me please.
Thank you in advance.
my database: 'id', 'content', 'image', 'date_post',
etc. and I try to use
$today = getdate();
$thisyear=$today['year'];
$thismon=$today['mon'];
$date_start=$thisyear.'-'.$thismon.'-01';
$date_end=$thisyear.'-'.($thismon+1).'-01';
$sql="SELECT *, DATE_FORMAT(date_post, '%d-%m-%Y') AS datepost
FROM my_table
WHERE date_post BETWEEN date('$date_start')
AND date('$date_end')
ORDER BY date_post DESC";
It makes with one query in MySQL, without any PHP:
SELECT * FROM `table_name`
WHERE DATE(`date_post`) >= DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, CONCAT('%Y-%m-', DAY(LAST_DAY(CURDATE() - INTERVAL 1 MONTH))))
AND DATE(`date_post`) <= DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH, '%Y-%m-01');
Ensuring that the query will not scan the full table but will use the index of date_post (if there is one!):
SELECT *
FROM myTable
WHERE date_post < LAST_DAY(CURDATE())
+ INTERVAL 2 DAY
AND date_Post >= LAST_DAY( LAST_DAY( CURDATE()) - INTERVAL 1 MONTH )
If it is run today ( 2011-07-01 ), it will give all datetimes between 2011-06-31 00:00:00 and 2011-08-01 23:59:59.