i have table which has column completed_at type (datetime)
i am inserting date in it by php date('Y-m-d H:i:s') 2016-05-17 00:00:14
I want to select data which has 3 hours ago
<?php
$sql = "Select `id`,`file` from `uploader` where `completed_at` < CURDATE() - INTERVAL 3 HOUR";
$DB = new DB();
mysqli_query($DB->db_connect(), $sql);
?>
but it gives me wrong answer.
Try this:
SELECT FROM table_name
WHERE completed_at < NOW() - INTERVAL 3 HOUR
Related
I need to consult a table using mysql...for today, tomorrow, after tomorrow,..
by now I use the following code where data is the day in format YYYY-MM-DD
$data_query = mysqli_query($conexionbd,'select * from `Meteo` where `data` >= "2017-03-31" ');
what should I insert in where data >= in order to get the data for today, tomorrow...without inserting manually the date?
You can it easy calculate with:
SELECT DATE (NOW() + INTERVAL 1 DAY);
sample
mysql> SELECT DATE (NOW() + INTERVAL 1 DAY) as result;
+------------+
| result |
+------------+
| 2017-03-30 |
+------------+
1 row in set (0,00 sec)
mysql>
To get tomorrow's date:
$tomorrow = date("Y-m-d", strtotime("+ 1 day"))
Now you can add that variable to your query:
$data_query = mysqli_query($conexionbd,'select * from `Meteo` where `data` >= "'.$tomorrow.'" ');
Then you can simply add more days as needed:
$after_tomorrow = date("Y-m-d", strtotime("+ 2 day"))
$DTtoday = new DateTime(); //Current date
$DT2days = clone $DTtoday; //Create new object from $DTtoday
$DT2days->add(new DateInterval("P2D")); //Add 2 days
$data_query = mysqli_query($conexionbd,'select * from `Meteo` where `data` BETWEEN "'.$DTtoday->format("Y-m-d").'" AND "'.$DT2days->format("Y-m-d").'"');
You could use now()
and between interval 2 days
data_query = mysqli_query($conexionbd,'select *
from `Meteo`
where `data` between now() and date_add(date(now() ), interval 2 day)) ;
Hi there :) I am doing a query, and it will pull results where the timestamp is more than 30 days old. Here's what I've put so far:
$result = $db->query("SELECT time FROM table1 WHERE open_time < (NOW() - INTERVAL 30 DAYS)");
So as you can see, it will try to find results more than 30 days old, however when I execute the query, it doesn't select any rows, even though the column field "open_time" is more than 30 days old.
Any suggestions I do greatly appreciate :)
Have you tried to cast the date field?
$result = $db->query("SELECT time FROM table1 WHERE CAST(open_time AS DATE) < (NOW() - INTERVAL 30 DAYS)");
EDIT:
Maybe you should use this tricky query:
$result = $db->query("SELECT time FROM table1 WHERE open_time <
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))"):
Try this Query
$result = $db->query("SELECT time FROM table1 WHERE DATE(`date`) = DATE(NOW() - INTERVAL 30
DAY")
or
try this one also for your reference
timestampadd()
SELECT * FROM table WHERE `date` > timestampadd(day, -30, now());
try this link also
$qry = "SELECT * FROM school WHERE WEEKOFYEAR(date) = WEEKOFYEAR(NOW()) ";
This query that i have currently in my code is getting me data from the current week starting from Monday. How can i get data from the table from last week or the week before. I have tried changing now to last week or currentweek.
any ideas is it possible to use weekofyear(()) and tweak it
You can do some date shift right in mysql, replace NOW() with
NOW() - INTERVAL 1 WEEK
there is also date_sub for subtracting
date_sub(NOW(),INTERVAL 1 WEEK)
Try this.
$qry = "SELECT * FROM school WHERE studId = $sessionId AND studentId = $student
AND date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY"
I have 2 tables:
profile userid, fname,lname
schedule: userid, date, start_time, end_time
Here is my query .
select *, group_CONCAT(distinct(date),'_',start_time,'_',end_time)
as dateformat FROM profile, schedule WHERE profile.userid =
schedule.pid
This returns all dates from the table. But what I am trying to do is return only certain days like:
date >= DATE(NOW()) and date <= date(now() + 2 day)
So today tomorrow and the day after.
No matter what I try it just returns all dates in the schedule database. I need this to return 2 rows userid 5 has 1 row in profile but 5 rows in schedule and userid 6 has 1 row in profile and 2 rows in schedule the query works fine its just returning all dates and not the dates within the
"date >= DATE(NOW()) and date <= date(now() + 2 day)"
did you try GROUP BY?
like WHERE profile.userid = schedule.pid GROUP BY date
select profile.userid,schedule.userid, group_CONCAT(distinct(date),'_',start_time,'_',end_time) as dateformat FROM profile, schedule WHERE profile.userid = schedule.userid and date >= DATE(NOW()) and date <= date(now() + interval 2 day);
That did it for now!! Now I can use the dateformat variable in php to parse out my results.
kanishka panamaldeniya - thanks for your suggestion!
Here is a query to get all rows in the past month.
$time = time() - 9676800;
$q = $this->db->query("
select id
from ipAddress
where date > {$time}
");
But how can i adjust this query to get all rows BESIDES the past month? Basically I want to end up deleting all rows over 1 month old
So long as your methodology for calculating "last/past month" satisfies you, then it's simple:
where date <= {$time}
You can use date_format, date_sub functions to get last month's dates.
Find answers here:
mysql last month date statement
and here
MySQL Query to calculate the Previous Month
You can do it like so
$time = strtotime('-1 Month');
$q = $this->db->query("
select id
from ipAddress
where `date` <= {$time}
");
but if date is a TIMESTAMP, DATE, or DATETIME string like 2013-02-27 22:16:38 or 2013-02-27 then you need something like
$time = date('Y-m-d H:i:s', strtotime('-1 Month'));
$q = $this->db->query("
select id
from ipAddress
where `date` <= '{$time}'
");
Or purely in SQL
select id
from ipAddress
where `date` <= DATE_SUB(NOW(), INTERVAL 1 MONTH)
And if I remember correctly, date is a reserved mysql word so use backticks in your sql.