[英]Get stats on time difference between 2 dates
I have 2 tables in MYSQL. The first contain all my bookings with the date the booking was made and the second contain all my tours available dates in datetime format.我在 MYSQL 中有 2 个表。第一个包含我所有的预订以及预订日期,第二个包含我所有可用日期时间格式的旅行日期。
This is an example how it looks like: 4 booking made for 2 different tours.这是一个示例:为 2 次不同的旅行进行了 4 次预订。
Table: myBooking表:我的预订
ID DATEADD IDTOUR
1 2020-08-01 13:30:00 1
2 2020-08-02 13:30:00 1
3 2020-08-03 13:30:00 2
4 2020-08-09 13:30:00 2
Table: myTours表:我的旅游
ID TOUR
1 2020-08-10 13:30:00
2 2020-08-11 13:30:00
I would like to have in PHP/MYSQL an overview of the time interval in which the customers book.我想在 PHP/MYSQL 中包含客户预订时间间隔的概览。
I would like to display:我想显示:
For the bookings made less than 24 hours, the total number of booking that were made less than 1 hour before the date of the tour, between 1 hour and 2 hours etc...对于少于 24 小时的预订,在旅行日期前不到 1 小时、1 小时和 2 小时之间等的预订总数...
Then, for the booking made more than 24 hours, the number of days between the booking and the date of the tour.然后,对于超过 24 小时的预订,预订与旅行日期之间的天数。
I find out DATEDIFF to get time interval between the 2 dates but i need help in order to complete the query and correctly display the results.我发现 DATEDIFF 可以获取两个日期之间的时间间隔,但我需要帮助才能完成查询并正确显示结果。
SELECT DATEDIFF(DATEADD, TOUR) as total FROM myBooking JOIN myTours ON myBooking.IDTOUR = myTours.ID
the result should look like:结果应该是这样的:
< 1hour : (number of bookings made less than 1hour before the tour)
1-2hours : (number of bookings made between 1-2hours before the tour)
3-4hours : (number of bookings made between 3-4hours before the tour)
etc untill 24h...等到24小时...
2 days : (number of bookings made 2 days before the tour)
3 days : (number of bookings made 3 days before the tour)
etc until 30 days...等到30天...
I find out DATEDIFF to get time interval between the 2 dates but i need help in order to complete the query and correctly display the results.
我发现 DATEDIFF 可以获取两个日期之间的时间间隔,但我需要帮助才能完成查询并正确显示结果。
DATEDIFF is not applicable for this task because: DATEDIFF 不适用于此任务,因为:
The solution may be, for example (schematically):解决方案可能是,例如(示意性地):
SELECT ...
CASE WHEN TIMESTAMPDIFF(HOUR, booking.date, tour.date) < 24
THEN CONCAT(TIMESTAMPDIFF(HOUR, booking.date, tour.date), ' hours')
ELSE CONCAT(TIMESTAMPDIFF(DAY, booking.date, tour.date), ' days')
END AS IntervalLength
...
GROUP BY IntervalLength
...
This will give statistic for each separate hour/day.这将为每个单独的小时/天提供统计信息。
If you need strictly described statistic periods (<1, 1-2, 3-4, ...) then create separate table like:如果您需要严格描述的统计周期(<1、1-2、3-4,...),则创建单独的表,如:
CREATE TABLE periods ( hours_from INT, hours_till INT, name INT );
INSERT INTO periods VALUES
(0,0,'<1 hour'),
(1,2,'1-2 hours'),
...
(24,47,'1 day'),
...
(720,743,'30 days'),
(744,99999999,'over 30 days');
join it to your table by加入你的桌子
ON TIMESTAMPDIFF(HOUR, booking.date, tour.date)
BETWEEN periods.hours_from
AND periods.hours_till
and group by periods.name
.并按
periods.name
分组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.