I am in problem in mysql database data retrieving. I have to get latest data inserted within a week or latest 7 days. I just know get data of specific date, but no within a span of days.
Please anyone help me. I am new in mysql.
You are looking for INTERVAL. For example, this will find all users whose created_time is in last 7 days and you have field created_time to tracked date of creation of record
SELECT * from users where created_time > (NOW()-INTERVAL 7 DAY)
You can do it using below query.
SELECT *
FROM `table`
WHERE `date` BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND
CURDATE()
OR
SELECT * FROM `table` WHERE `date` >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
OR
SELECT * FROM table WHERE DATEDIFF(NOW(),dateField) < 7
we can use DATE_SUB Mysql default function
select * from yourtable where date_of_insertion >= DATE_SUB(NOW(),INTERVAL 7 DAY)
<?php
$date = date("your_date_format", strtotime(- 7 days));
$result = mysqli_query($link, "SELECT * FROM table WHERE `date` > '$date'");
?>
Simple as that.
try :
select * from tablename where add_time >= now() - interval 7 day
Related
I am trying to pull amounts from the pay column of the loads table for the last 7 days and echo it to show results. i have tried several different way to do this and i am stumped.
<?PHP
$startDate = date("Y-m-d");
$endDate = strtotime(date("Y-m-d"). "-7 days");
$lsql ="SELECT * FROM loads WHERE created_at BETWEEN '$startDate 00:00:00' AND '$endDate 23:59:59'";
foreach ($link->query($lsql) as $ldata) {
echo $ldata['pay'];
}
?>
You probably just need to do:
SELECT * FROM loads WHERE created_at >= CURDATE() - INTERVAL 7 DAY;
directly from MySQL query. Or use DATE_SUB like:
SELECT * FROM loads WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
If CURDATE() is not working, change it to NOW() like:
SELECT * FROM loads WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
So I want to purge some data from the system which is older then 2 years.
I have a datetime field where I store.
I know I can do something like
SELECT * FROM `table` where date_field < (??? what goes here)
My problem is i think how do I calculate the date, or maybe I am confused
For a specific answer in case the Manual isn't clear enough:
SELECT * FROM `table` where date_field < (now() - interval 2 year)
Use DATE_SUB
SELECT * FROM `table`
where date_field < DATE_SUB(CURDATE(), INTERVAL 2 YEAR)
may this can help :
SELECT EXTRACT(YEAR FROM `date`) AS year
from table
having year< (EXTRACT(YEAR from CURDATE())-2)
You can use INTERVAL:
SELECT * FROM `table` WHERE date_field < DATE_SUB(CURDATE(),INTERVAL 2 YEAR);
Referenced from https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html.
SELECT * FROM table where date_field < DATE_SUB(NOW(),INTERVAL 2 YEAR);
I am trying to retrieve data for the passed 24 hours but I am getting nothing, but when I check on the table column (days) directly in db, I can find them. Here is my query:
select * from mytable
where
days between concat(date(date_sub(now(), interval 1 day))
between requires 2 arguments that specify the range:
select * from mytable
where days between date_sub(now(), interval 1 day) and now();
Or simply
select * from mytable
where days > date_sub(now(), interval 1 day);
You can use
select * from mytable where days > date_sub(CURDATE(),INTERVAL 1 DAY);
I'm sorry if this sounds like a very basic question but for some reason, today I'm really having trouble getting my head round this. I have a database table with a date_added column in the format of 2014-09-30 20:39:17 and I have a web page with filter options for users. Basically I want to use variables to select different date ranges like so:
SELECT * FROM table WHERE date_added = /* EVERYTHING POSTED TODAY */
SELECT * FROM table WHERE date_added = /* EVERYTHING POSTED WITHIN LAST 7 DAYS */
SELECT * FROM table WHERE date_added = /* EVERYTHING POSTED WITHIN LAST 30 DAYS */
What would I need to put in to get those variables to work?
You can use CURDATE() and very simple INTERVAL arithmetic.
In the following examples assume that query was executed at 2014-10-21 22:25:28:
SELECT * FROM table WHERE date_added >= CURDATE()
-- >= 2014-10-21 00:00:00
SELECT * FROM table WHERE date_added >= NOW() - INTERVAL 24 HOUR
-- >= 2014-10-20 22:25:28
SELECT * FROM table WHERE date_added >= CURDATE() - INTERVAL 7 DAY
-- >= 2014-10-14
SELECT * FROM table WHERE date_added >= CURDATE() - INTERVAL 30 DAY
-- >= 2014-09-21
I am working on an event management project, I need to get upcoming event from database, can anyone help me ?
I am using
SELECT *FROM EVENTS WHERE DATE_ADD(event_date, INTERVAL YEAR(CURDATE())-YEAR(event_date) YEAR)
BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY) LIMIT 1;
But it gives me the event upcoming within 7 days only.
I should not want to provide days.
I want to get first upcoming event , doesn't matter after or within how many days it is coming.
thank you !
Isn't this a simple where clause comparing with current date
SELECT *
FROM EVENTS
WHERE event_date > CURDATE()
ORDER BY event_date
LIMIT 1;
Wouldn't this work?
SELECT * FROM EVENTS WHERE event_date > CURDATE() ORDER BY event_date ASC LIMIT 1;
try
If column type datetime you can use CURDATE()(mysql function) or date()(php function)
"SELECT *FROM EVENTS WHERE event_date > '".date('Y-m-d H:i:s')."' ORDER BY event_date limit 1"
or
"SELECT *FROM EVENTS WHERE event_date > CURDATE() ORDER BY event_date limit 1"
If column type date
"SELECT *FROM EVENTS WHERE event_date > '".date('Y-m-d')."' ORDER BY event_date limit 1"
try below it works perfectly fine
select * from table where start_time between NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY)
you can use like this to get next 7 days data or in case you want last 7 days then instead of DATE_ADD USE DATE_SUB It will work or you can use like
start_time between NOW() AND DATE_ADD(NOW(), INTERVAL 1 WEEK)
this also works. to know more check this ref https://dev.mysql.com/doc/en/date-and-time-functions.html
thank you everyone,
I got the answer:
SELECT * FROM EVENTS
WHERE event_date > CURDATE()
ORDER BY event_date ASC LIMIT 1;