SQL time period query - php

I need to select from a table all rows that have date_added between CURDATE() and 6 weeks ago.
Help please.

SELECT *
FROM mytable
WHERE date_added BETWEEN CURDATE() - INTERVAL 6 WEEK AND CURDATE()

SELECT *
FROM a_table
WHERE date_added BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 WEEK) AND CURDATE()

SELECT * FROM table_name WHERE TO_DAYS(NOW()) - TO_DAYS(date_added) <= 42

SELECT * FROM table_name WHERE DATEDIFF(NOW(),date_added)<=42

if you use dates in one year
select date_format(date, '%u') from tab
where (date_format(date, '%u')-date_format(now(), '%u'))>6
if you use dates with different years
you don't have to use dates with another year.
you can use
select
date_format(date, '%u') from tab
where (date_format(date, '%u')-date_format(now(), '%u'))>6
and
date_format(date, '%u') from tab
where (date_format(date, '%Y')-date_format(now(), '%Y'))=0
you can optimize query with join if you want. I think you know how to do it

Related

MYSQL select data that is older then 2 years

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);

How to get sum for different time period in one query

query 1:
SELECT SUM(duration) FROM table_name WHERE timestart >= past24hours
query 2:
SELECT SUM(duration) FROM table_name WHERE timestart >= past7days
Hi all, i want to combined above two query and get sum in query.
Any way to do this?
Currently i have 2 more query in addition to above for past 30 days and 365 days.
Anyone please help.
You can use union
like
SELECT SUM(duration) FROM table_name WHERE timestart >= past24hours
UNION
SELECT SUM(duration) FROM table_name WHERE timestart >= past7days
SELECT SUM(duration), 1 FROM table_name WHERE timestart >= '$date' - INTERVAL 1 DAY
UNION
SELECT SUM(duration), 2 FROM table_name WHERE timestart BETWEEN '$date' - INTERVAL 7 DAY AND '$date' - INTERVAL 1 DAY
this way you wont get rows with timestart in last 24 hours twice
One way is to get all sums with different alias is as
select
t.sum1,t.sum2
from(
select
sum(case when timestart >= past24hours then duration END) as sum1,
sum(case when timestart >= past7days then duration END) as sum2
from table_name
)t
You can add any number of sums within the query and give an alias name and add them in the select list so that they could be parsed later with your server side script.
With UNION it will return the same column/alias. When you have same sum value in UNION it will get only one and to get rid of it you can use UNION ALL

Specify PHP code to select dates and records 3 days old

I am trying to call data from SQL table that is only 3 days old
My table has a lbs-date column in it and is date format. I have tried the following but get no result from the query at all
$result = mysql_query("SELECT *, DATE_FORMAT(datetime, '%y,%m,%d') FROM lbs_trace_etrack
WHERE lbs_date(datetime) = CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC")
Is there any other way I can call only the last 3 days of information from the SQL my date format is Y/M/D
SELECT *, DATE_FORMAT(lbs_date, '%y,%m,%d')
FROM lbs_trace_etrack
WHERE lbs_date >= CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC
check DATE_FORMAT. Its syntax is DATE_FORMAT(<date>,format) . Use like this :
SELECT *, DATE_FORMAT(lbs_date , '%y,%m,%d') FROM lbs_trace_etrack
WHERE lbs_date = CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC

Get data from database inserted in latest 7 days

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

Get one day old record from database

How can i get one day old record from database from server . i have use datetime to insert record. below is how record is look like.
2013-01-15 23:44:02
i have use strtotime('-1 day') but it returns local system time.i want to get one day old record and do some stuff in condition..
Thanx in advance..
where date(date_column) = DATE_sub(NOW(), INTERVAL 1 DAY);
use DATE_ADD
SELECT *
FROM tablename
WHERE DATE(dateCol) = DATE_ADD(CURDATE(),INTERVAL -1 DAY)
DATE_ADD()
SELECT * from table WHERE datefield < DATE_SUB(NOW(), INTERVAL 1 DAY)
You will be archiving this with sql query.
SELECT * FROM tablename WHERE DATE(datefield)=DATE_ADD(CURDATE(),INTERVAL 1 DAY)
CURDATE() will return the current Mysql Server time.
You can use this function DATE_SUB(date,INTERVAL expr type)
http://www.w3schools.com/sql/func_date_sub.asp
Example provided in W3schools
Assume we have the following "Orders" table:
Columns: OrderId, ProductName, OrderDate
Values: 1, Jarlsberg Cheese, 2008-11-11 13:23:44.657
Now we want to subtract 5 days from the "OrderDate" date.
We use the following SELECT statement:
SELECT OrderId,DATE_SUB(OrderDate,INTERVAL 5 DAY) AS SubtractDate
FROM Orders
$yesterday = strtotime('Y-m-d h:i:s','yesterday');
SELECT * FROM tablename WHERE datefield = '$yesterday'

Categories