Fetch Previous Week Records from MySql - mysql

I have a MySql table having a lot of records. But i want to fetch seperate records of current & previous week.
Query for Current Week Records which works perfectly
SELECT * FROM tbl_track WHERE WEEK(`date_created`) = WEEK(CURDATE())
Query for Previous Week but it dosn't work
SELECT * FROM tbl_track WHERE WEEK(`date_created`) = WEEK(CURDATE()) INTERVAL 7 DAY

SELECT * FROM tbl_track
WHERE WEEK(`date_created`) = WEEK(DATE_SUB(CURDATE(), INTERVAL 7 DAY))
OR:
SELECT * FROM tbl_track
WHERE WEEK(`date_created`) = WEEK(CURDATE() - INTERVAL 7 DAY)

That doesnt look like an index can be used but thats a side issue really. You want your curdate to be - 7 days
SELECT * FROM tbl_track WHERE WEEK(date_created) = WEEK(ADDDATE(CURDATE(), INTERVAL -7 DAY));

Related

How to select last 7 days available - MySql

I am having problem with dealing with data in MySQL. How can I select all data from the pasty seven available data?
I tried to run
SELECT * FROM database where ship_day
BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW();
but I doesn't work for my purpose.
Let's say we are on January, 9th and my MAX(ship_day) is January, 7th and I want to pull data from the past 7 available data so from January 1st to 7th.
I tried to run
SELECT * FROM database where ship_day
BETWEEN DATE_SUB(MAX(ship_day), INTERVAL 7 DAY) AND MAX(ship_day);
but I got an error.
I know that I can increase INTERVAL 7 DAY to INTERVAL 9 DAY but I want this process to be automatic. How can I solve?
Well, here is one method:
SELECT d.*
FROM database d
WHERE d.ship_day >= COALESCE( (SELECT DISTINCT d2.ship_day
FROM database d2
ORDER BY d2.ship_day DESC
LIMIT 1 OFFSET 6
), d.ship_day
);
Note: If you have only one row per ship_day, then just use limit:
SELECT d.*
FROM database d
ORDER BY d.ship_day DESC
LIMIT 7
Let's say we are on January, 9th and my MAX(ship_day) is January, 7th
and I want to pull data from the past 7 available data so from January
1st to 7th.
I assume you are looking for the query below but hard to say for sure without example data and expected results.
SELECT
*
FROM
t
WHERE
ship_day >= (
SELECT
MAX(ship_day) - INTERVAL 7 DAY
FROM
t
)
Or you can also write this as
SELECT
*
FROM
t
WHERE
ship_day >= (
SELECT
MAX(ship_day)
FROM
t
) - INTERVAL 7 DAY
You can get the last date for which you have data with:
select MAX(ship_day) FROM database
so use it like this:
SELECT * FROM database
where ship_day >= DATE_SUB((select MAX(ship_day) FROM database), INTERVAL 7 DAY)

SQL Query by created 3 hours ago

Given a set of data with date_created stored like 2017-04-13 23:29:52, how would I construct an SQL query to select all items that were created within the last 3 hours?
I originally thought to do something like this:
SELECT
*,
MAX(date_created)
FROM items
GROUP BY date_created
but that would not be exactly what I want. I'm not sure how to go about this.
Use NOW() and INTERVAL in your WHERE clause
SELECT * FROM items WHERE date_created <= NOW() - INTERVAL 180 minute AND date_created >= NOW() - INTERVAL 210 minute
This one uses CURDATE, CURDATE and DATE_ADD:
SELECT *
FROM items
WHERE DATE(date_created) = CURDATE()
AND TIME(date_created) BETWEEN CURTIME() AND DATE_ADD(CURTIME(), INTERVAL -3 HOUR)
select * from items where
extract(hours from age(current_timestamp, date_created))>=3;
Extract keyword would extract hours difference from current timestamp and return only that is greater than or equal to 3.

How to select values from 2 hours ago from now

I am not having much knowledge on mysql queries.. and i'm trying to get values from database from last two hours from now ..I have searched about it and i have found some related posts too.. but unfortunately i am unable to implement the logic.. please help me solve this issue..
here is the mysql query
select *
from `ordermaster`
where ((
`ordermaster`.`Pick_date`
= curdate())
and (`ordermaster`.`Pick_time`
<= (now() - interval 2 hour))
and (`ordermaster`.`Status`
= 2))
were, Pick_Date = "2017-04-19" (today date) and Pick_Time = "10:00:00" (24 hours format)
Try the Following:
Select * From ordermaster om
Where Concat(om.Pick_date,' ', om.Pick_time) as date Between
(now() - interval 2 hour) and now()
AND Status = 2
You can use the between keyword
select *
from `ordermaster`
where `Pick_date` = curdate() and
`Pick_time` between (now() - interval 2 hour) and now() and
`Status` = 2
Edit
Based on our chat, where we found out the GoDaddy server you have your db hosted on is 12.5 hours ahead of your local time, the final query you should use is
select *
from `ordermaster`
where `Pick_date` = curdate() and
`Pick_time` <= now() + interval 10 hour + interval 30 minute and
`Status` = 2

Mysql Add DATE_COLUMN 3 DAYS with query

I've been asking many question here, but it was dead end, I just want to make a simple select from date_column+3 day, I'm new to MySQL.
I've tried with this query
SELECT *
FROM pemesanan
WHERE pemesanan.`date` = DATE(DATE_ADD(date, INTERVAL 3 DAY))
but the result is empty, here's my column
date
2016-08-10
2016-08-04
2016-08-07
it must be show the 2016-08-10 data, but its not,
Can somebody enlighten me, I've been frustrated with this
If you use date right side you should use date left side
SELECT *
from pemesanan
where date(pemesanan.`date`) = DATE(DATE_ADD(NOW(), INTERVAL 3 DAY))
If you use the same date pemesanan.date you never get pemesanan.date = pemesanan.date +3
but if you want select the date older than 3 day you shuold use
SELECT *
from pemesanan
where date(pemesanan.`date`) <= DATE(DATE_sub(NOW(), INTERVAL 3 DAY))
and for delete
DELETE from pemesanan
where date(pemesanan.`date`) <= DATE(DATE_sub(NOW(), INTERVAL 3 DAY))
in your case
SELECT *
from pemesanan
where date(pemesanan.tanggal) <= DATE(DATE_sub(NOW(), INTERVAL 3 DAY))

Add Week Interval to Date Query

I have the following SQL Query which delivers data well for current date, how can i set this query to query a week of data from curdate?
SELECT count( * ) as today_total_4
FROM cdr
WHERE dcontext='ext-queues' AND DATE(`calldate`) = DATE(CURDATE())
Many Thanks.
You can use a DATE_ADD function to get the date a week from now. (Either forward or back)
http://www.w3schools.com/sql/func_date_add.asp
SELECT count( * ) as today_total_4
FROM cdr
WHERE dcontext='ext-queues' AND DATE(`calldate`) = DATE_ADD(CURDATE(), INTERVAL 7 DAY)
Depending on your query requirements you can calculate 7 days back using:
DATE_ADD(CURDATE(), INTERVAL -7 DAY)
If you wanted your query to be everything since a week ago, then you'd change your query to have the following where clause:
WHERE dcontext='ext-queues' AND DATE(calldate) > DATE_ADD(CURDATE(), INTERVAL -7 DAY)
SELECT count( * ) as today_total_4
FROM cdr
WHERE dcontext='ext-queues' AND WHERE WEEK(`calldate`) = WEEK(DATE(CURDATE()))