Join on all dates within the range MySQL - php

I have the following table :
administratieID | varchar
getalTypeNaam | varchar
datum | date
waarde | double
With the following query:
Select administratieID AS AI, waarde WA, datum DT
from getalType
where getalTypeNaam = 'test'
and datum between DATE_SUB(NOW(), INTERVAL 10 DAY) and DATE_SUB(NOW(), INTERVAL 15 DAY)
order by datum
What I want is that I can do a select where all the dates between the specified dates are returned even if there is nothing in the database that coresponds with that date. I want this even if there is no value but in that case i want to return the value 0.
So I want this in return:
date administratieID waarde
10-10-2016 xxx 10
11-10-2016 xxx 0 <-- no data available
12-10-2016 xxx 40
13-10-2016 xxx 9
14-10-2016 xxx 0 <-- no data available
Now I get this:
date administratieID waarde
10-10-2016 xxx 10
12-10-2016 xxx 40
13-10-2016 xxx 9

There are ways to do this, but they are quite hackish...
One would be to have an inline incrementing variable, and use a dummy table that you know has at least as many records in it as the maximum number of days in your date range. Then you can do something like this:
SET #date = DATE_SUB(NOW(), INTERVAL 15 DAY); #or whatever the earliest date you need is
SELECT DISTINCT
IFNULL(administratieID,'XXX') AS AI
, IFNULL(waarde,0) AS WA
, all_dates AS DT
FROM
(SELECT #date:=DATE_ADD(#date, INTERVAL 1 DAY) AS all_dates
FROM
my_dummy_table_with_lots_of_records
WHERE #date >= DATE_SUB(NOW(), INTERVAL 15 DAY) AND #date <= DATE_SUB(NOW(), INTERVAL 10 DAY)) d LEFT JOIN
getalType t ON
DATE_FORMAT(all_dates,'%Y-%m-%d') = DATE_FORMAT(t.datum,'%Y-%m-%d')
WHERE
IFNULL(getalTypeNaam, 'Test') = 'Test' #if there's no data we still need to return something
ORDER BY all_dates;
Another easier way is just to have a table called "all_dates", which you can use in the join. The table would just have one column, "datum" and you populate it with every possible date that could be used in the getalType table. This could even be done with a trigger.
Then you link to this table, instead of doing all the derived nonsense above.
This would also avoid having to have multiple sql statements, as you don't have a variable that needs initialising.

Related

Select date with n days from the date added

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)

Mysql query based on date?

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

MySQL BETWEEN DATE RANGE

I have a scenario where I need to pull up delivery dates based on a table below (Example)
job_id | delivery_date
1 | 2013-01-12
2 | 2013-01-25
3 | 2013-02-15
What I'm trying to do is show the user all the delivery dates that start with the earliest (in this case it would be 2013-01-12) and add an another 21 days to that. Basically, the output I would expect it to show of course, the earliest date being the starting date 2013-01-12 and 2013-01-25. The dates past the February date are of no importance since they're not in my 21 date range. If it were a 5 day range, for example, then of course 2013-01-25 would not be included and only the earliest date would appear.
Here is main SQL clause I have which only shows jobs starting this year forward:
SELECT date, delivery_date
FROM `job_sheet`
WHERE print_status IS NULL
AND job_sheet.date>'2013-01-01'
Is it possible to accomplish this with 1 SQL query, or must I go with a mix of PHP as well?
You can use the following:
select *
from job_sheet
where print_status IS NULL
and delivery_date >= (select min(delivery_date)
from job_sheet)
and delivery_date <= (select date_add(min(delivery_date), interval 21 day)
from job_sheet)
See SQL Fiddle with Demo
If you are worried about the dates not being correct, if you use a query then it might be best to pass in the start date to your query, then add 21 days to get the end date. Similar to this:
set #a='2013-01-01';
select *
from job_sheet
where delivery_date >= #a
and delivery_date <= date_add(#a, interval 21 day)
See SQL Fiddle with Demo
SELECT date,
delivery_date
FROM job_sheet
WHERE print_status IS NULL
AND job_sheet.date BETWEEN (SELECT MIN(date) FROM job_sheet) AND
(SELECT MIN(date) FROM job_sheet) + INTERVAL 21 DAY
SELECT j.job_id
, j.delivery_date
FROM `job_sheet` j
JOIN ( SELECT MIN(d.delivery_date) AS earliest_date
FROM `job_sheet` d
WHERE d.delivery_date >= '2013-01-01'
) e
ON j.delivery_date >= e.earliest_date
AND j.delivery_date < DATE_ADD(e.earliest_date, INTERVAL 22 DAY)
AND j.print_status IS NULL
ORDER BY j.delivery_date
(The original query has a predicate on job_sheet.date; the query above references the d.delivery_date... change that if it is supposed to be referencing the date column instaed.)
If the intent is to only show delivery_date values from today forward, then change the literal '2013-01-01' to an expression that returns the current date, e.g. DATE(NOW())

curdate interval for a int field?

I want to select records from the last 3 years...
the following use to work when the table column type 'released_year' was a date
$query = 'SELECT
album.album_id,
album.title,
album.released_year,
FROM album
WHERE album.released_year >= ( CURDATE() - INTERVAL 3 YEAR )
ORDER BY album.released_year DESC, album.title';
but the table column type change and it's now a type smallint to handle only the 4 digits of a year.
How do i select records from the last 3 years now?
... album.released_year >= year(CURDATE()) -3 ...
ugly as you're not using real dates, but it will work for a while

mysql select only next week data

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

Categories