5

I want to display all the dates between two dates for a particular record

And this is the table :

ID Start_Date End_Date
-------------------------
1 2013年01月14日 2013年01月18日
2 2013年02月01日 2013年02月04日

and now i want to get all the dates between from date and to date.

Expected output

ID Date
-------------
1 2013年01月14日
1 2013年01月15日
1 2013年01月16日
1 2013年01月17日
1 2013年01月18日
2 2013年02月01日
2 2013年02月02日
2 2013年02月03日
2 2013年02月04日

guide me to write query for that with out creating any extra tables.

I have already tried this following query

select * from 
(select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) selected_date from
 (select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between '2012-02-10' and '2012-02-15'

its working fine for single record. but i want get all the date intervals from my table

Updated

i have 6 qty of chairs for all days. So one user book 3 chars on 2013年01月14日 to 2013年01月18日 another person book 2 chars on 2013年01月17日 to 2013年01月20日. So my expected output shown below.

ID Date Available
------------------------
1 2013年01月14日 3
1 2013年01月15日 3
1 2013年01月16日 3
1 2013年01月17日 5 
1 2013年01月18日 5
1 2013年01月19日 2
1 2013年01月20日 2 
1 2013年01月21日 2
joanolo
13.7k8 gold badges39 silver badges67 bronze badges
asked May 11, 2015 at 11:40
4
  • It's look like a bad design for me. It is easy enough to check whether or not the given date belongs to the any predefined range, but it is not so reasonable to generate the whole set of possible dates. IMHO, sure. Commented May 11, 2015 at 12:54
  • @AbhikChakraborty my Schema sqlfiddle.com/#!9/73217 Commented May 11, 2015 at 13:37
  • @AbhikChakraborty my expected output is given above. Available means remaining rooms Commented May 11, 2015 at 13:43
  • Question from the sqlfiddle, how you have 1 2013年01月14日 3 , in the property table there are 6 rooms and in the orders table (1, 1, '2013年01月14日','2013年01月18日',2) for that property id 2 are booked. What is the logic of getting 3 since it does not tell the remaining 6-2 Commented May 11, 2015 at 14:09

2 Answers 2

4

The easiest way is to have a calendar table, defined in the following way:

CREATE TABLE calendar
(
 a_day date PRIMARY KEY
) ;

... and filled with all the relevant dates (i.e.: all days from 1990年1月1日 to 2100年12月31日). For the sake of simplicity, we will fill it only with year 2013:

INSERT INTO 
 calendar (a_day)
VALUES
 ('2013-01-01'),
 ('2013-01-02'),
 ('2013-01-03'),
 ('2013-01-04'),
 ('2013-01-05'),
 -- everything up to
 ('2013-12-31') ;

At this point, you can just have a JOIN with the two tables; with the join condition not being an equality, but a range condition:

SELECT
 t.id, c.a_day
FROM
 t
 JOIN calendar c ON c.a_day BETWEEN t.start_date AND t.end_date 
ORDER BY
 t.id, c.a_day ;

... and get

id | a_day 
-: | :---------
 1 | 2013年01月14日
 1 | 2013年01月15日
 1 | 2013年01月16日
 1 | 2013年01月17日
 1 | 2013年01月18日
 2 | 2013年02月01日
 2 | 2013年02月02日
 2 | 2013年02月03日
 2 | 2013年02月04日

You can see all the setup at dbfiddle here

answered Jul 8, 2017 at 21:19
0

This query uses the date function because column created_at contains both date and time.

SELECT date(`created_at`)
FROM table_name
WHERE date(`created_at`) BETWEEN '2018-03-08' AND '2018-03-22'
GROUP BY DATE(`created_at`)
Andriy M
23.3k6 gold badges60 silver badges104 bronze badges
answered Mar 26, 2018 at 11:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.