I have a table like this
id | date | content
1 | 09-16-2013 | content 1 here
2 | 09-23-2013 | content 2 here
3 | 09-30-2013 | content 3 here
I would like to display the content for a week from that date. For example, the first content should start on 9/16/2013 and then show until 9/22/2013 mid night. then on next day, it changes to the content 2.
Same way,when I am on content 2, I want to display like "previous week content" and then show just the previous ones..I think I can do this by checking the current date and then anything below that has to be displayed.
I am not very good at these kind of mysql queries, please advise!
Regards
I guess you're looking for something like this
SELECT *
FROM table1
WHERE date BETWEEN CURDATE() + INTERVAL 0 - WEEKDAY(CURDATE()) DAY
AND CURDATE() + INTERVAL 6 - WEEKDAY(CURDATE()) DAY
This query will grab a row(s) where date column is within the boundaries of the current calendar week (from Monday to Sunday).
WEEKDAY() function returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday). The expression
CURDATE() + INTERVAL 0 - WEEKDAY(CURDATE()) DAY
returns a date for Monday of the current calendar week and
CURDATE() + INTERVAL 6 - WEEKDAY(CURDATE()) DAY
returns a date for Sunday of the current calendar week.
Using BETWEEN in WHERE clause makes sure that a query returns only rows with date values that falls between these two dates (Monday through Sunday).
Note: Make sure that you have an index on date column. This query is index-friendly.
Sample output for today's date (09/19/2013):
+------+------------+----------------+
| id | date | content |
+------+------------+----------------+
| 1 | 2013-09-16 | content 1 here |
+------+------------+----------------+
UPDATE: To get records for previous calendar week you just substract 1 week interval from both values in BETWEEN
SELECT *
FROM table1
WHERE date
BETWEEN CURDATE() + INTERVAL 0 - WEEKDAY(CURDATE()) DAY - INTERVAL 1 WEEK,
AND CURDATE() + INTERVAL 6 - WEEKDAY(CURDATE()) DAY - INTERVAL 1 WEEK
Try this
SELECT * FROM table WHERE date BETWEEN '09-16-2013' AND '09-22-2013';
keyword is WEEK()
SELECT id,date, CONCAT('content ',WEEK(date),' to here') as content FROM table_name
SELECT *
FROM table
WHERE date BETWEEN '9/16/2013 00:00:00.00' AND '9/22/2013 00:00:00.00'
You can replace the week offset to your needs
SET #weekOffset = +2;
SELECT * FROM test
WHERE WEEK(`date`) = WEEK(NOW()) + #weekOffset;
See a working demo here
To select it dynamically, try something like
SELECT * FROM `yourTable` WHERE NOW() >= STR_TO_DATE(`date`, '%m-%d-%Y') ORDER BY STR_TO_DATE(`date`, '%m-%d-%Y') DESC LIMIT 1
or t
SELECT * FROM `yourTable` WHERE CURDATE() >= STR_TO_DATE(`date`, '%m-%d-%Y') ORDER BY STR_TO_DATE(`date`, '%m-%d-%Y') DESC LIMIT 1
sqlfiddle example - http://sqlfiddle.com/#!2/62982/4
Related
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
How do I get the first Sunday between 2 dates?
i have only 2 fields in myTable (dt_from and dt_to)
dt_from = '2016-08-6';
dt_to = '2016-08-19';
SELECT firstsunday FROM myTable BETWEEN dt_from AND dt_to;
How about getting the first Sunday just after from date itself?
SELECT DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) from myTable as A;
http://sqlfiddle.com/#!9/7fce5
If Sunday isn't between the dates:
SELECT DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) as firstsunday from myTable as A where DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) between A.date_from and A.date_to;
http://sqlfiddle.com/#!9/83d0f0/4
A little bit shorter:
SELECT firstsunday from (SELECT A.date_from, A.date_to, DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) as firstsunday from myTable as A) as B where B.firstsunday between B.date_from and B.date_to;
http://sqlfiddle.com/#!9/6adab3/1
Try this:
SELECT dt, DAYNAME(dt) as day
FROM myTable WHERE dt BETWEEN date1 AND date2
ORDER BY dt
WHERE day = "Sunday"
LIMIT 0,1
assume you only need to sue the db to get the date range so you dont want any king of query.
<?php
echo date('m/d/Y', strtotime('next Sunday', strtotime('2016-08-6')));
you can add some validation to make sure its before your end date
I have a mysql table with the following fields
Name | Email | Date | Status
I want to extract the records where date range is between 30 days
Assume today is 2014/12/9
ie. date values are
2014/11/25
2014/12/2
2014/12/1
2014/10/25
2014/11/9
I need the o/p as (the number of days should be with in 30 days from the db date to today date)
2014/11/25
2014/12/2
2014/12/1
2014/11/9
I want to extract records those have the interval of less than 30 days from the date in the db.
Yes. I want to fetch the record between 2 days. For this I used this query
SELECT * FROM tbl_jobboard WHERE dtDate <= ( dtDate +30 )
But it is not working.
How to write the select query?
USE DATE_SUB like this:
SELECT * FROM table1
WHERE `date` BETWEEN DATE_SUB(CURDATE(),INTERVAL 30 DAY) AND CURDATE()
Working Fiddle Demo: http://sqlfiddle.com/#!2/6344f2/1
use following query
select * from table_Name t where t.date<=now() and t.date>=DATE_SUB(now(),
INTERVAL 31 DAY)
i create a event for calculate a birthday , for example:
birthday date = 1990-09-07
now date = 2013-09-05
my query :
SELECT id FROM user WHERE ( birthday - NOW() ) <= 7
this query is mistake
I think DAYOFYEAR function is more suitable for you:
mysql> SELECT DAYOFYEAR('2000-09-07') - DAYOFYEAR(now()) AS diff;
+------+
| diff |
+------+
| 3 |
+------+
Today is 2013-09-05, it gave 3 days. Now you can compose the condition. Please, mind 1 day in the leap year.
You said its for birthday so you must consider month and date as date can be repeat for every month..
So try something like below, for year difference.
SELECT
(YEAR(birthdate) - YEAR(NOW())) AS yeardifference
FROM
table
WHERE
MONTH(birthdate) = MONTH(NOW())
AND
DATEDIFF(birthdate, NOW()) <= 7
For day difference
SELECT
DATEDIFF(birthdate, NOW())
FROM
table
WHERE
MONTH(birthdate) = MONTH(NOW())
AND
DATEDIFF(birthdate, NOW()) <= 7
The Dayofyear function would be useful, just to put it in context with your requirement
SELECT id FROM user WHERE (DAYOFYEAR(birthday) - DAYOFYEAR(NOW())) <= 7
I think your query should be something like
SELECT id
FROM user
WHERE ( DAYOFYEAR(birthday) - DAYOFYEAR(NOW()) <= 7)
I have a date field in my database i.e film_release_date.
So what is mysql query to fetch the next week data from table.
I run this query but its not working properlly.
SELECT
*,
DATE_FORMAT(film_release_date,'%e-%b-%Y') AS release_date
FROM gf_film
WHERE
MONTH(film_release_date)=MONTH(CURRENT_DATE)
AND YEAR(film_release_date)=YEAR(CURRENT_DATE)
AND film_release_date>=(CURRENT_DATE)
ORDER BY film_release_date DESC
Actually its for movie website i have several movies in my database so i need a query which fetch every next week movie that means movies that will release in next week means next friday.If i run this today i.e 13 then it shows all movies between 15 to 22 and if run this between 15 to 22 then it shows data from 22 to 29
set #d := date_add(CURRENT_DATE, interval 7 day);
set #week_start := #d - interval (dayofweek(#d) + 1) day;
set #week_end := #d + interval (6 - dayofweek(#d)) day;
SELECT *,
DATE_FORMAT(film_release_date,'%e-%b-%Y') AS release_date
FROM gf_film
WHERE film_release_date between #week_start and #week_end
ORDER BY film_release_date DESC
to get next week data you can simple apply below logic :
SELECT something FROM tbl_name
-> WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAYS)
REFERENCE
Little late, but none of the answers works properly. You can use yearweek function to get week number of the year. So you get the week number of now (this week) or now() + interval 7 day to get next week. Examples:
THIS WEEK
SELECT *, DATE_FORMAT(film_release_date,'%e-%b-%Y') AS release_date
FROM gf_film
WHERE YEARWEEK(film_release_date) = YEARWEEK(NOW())
ORDER BY film_release_date DESC
NEXT WEEK
SELECT *, DATE_FORMAT(film_release_date,'%e-%b-%Y') AS release_date
FROM gf_film
WHERE YEARWEEK(film_release_date) = YEARWEEK(NOW() + INTERVAL 7 DAY)
ORDER BY film_release_date DESC