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
2 Answers 2
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
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`)
1 2013年01月14日 3
, in theproperty
table there are 6 rooms and in theorders
table(1, 1, '2013年01月14日','2013年01月18日',2)
for that property id2
are booked. What is the logic of getting 3 since it does not tell the remaining6-2