gpt4 book ai didi

mysql - 重叠预订的最小和最大计数

转载 作者:行者123 更新时间:2023-11-29 21:37:46 24 4
gpt4 key购买 nike

我有一个预订系统,其中有多个同时预订,每个预订都有一个计数。我需要获取指定日期范围(在本例中为一天)的最小值和最大值。我发现了一些很好的代码 here ,在测试中效果很好。但在我的实现中,它在这个特定的实例中失败了:

  • 它不计算在查询范围之前开始并在查询范围内结束的预订。

如何解决这个问题?

这是一个例子:

此预订具有以下属性:列表(多个预订可以拥有的 ID,但在本例中只有一个):2f23f23f

开始日期:2016-01-15 08:00:00

结束日期:2016-01-17 08:00:00

状态:事件

数量:1

结果:

最小计数:0

最大计数:0

它应该返回:

最小计数:0

最大计数:1

如果我们查询完全相同的内容,但日期范围为 2016-01-16 00:00:00 - 2016-01-16 23:59:59,它会返回正确的答案:

最小计数:1

最大计数:1

这是 MYSQL:

SELECT
MAX(simultaneous) AS max_count,
MIN(simultaneous) AS min_count
FROM (
SELECT IFNULL(SUM(
(
CASE WHEN (
listings = '2f23f23f'
AND
(state = 'active')
)
THEN count
ELSE 0
END
)
),0) AS simultaneous
FROM bookings RIGHT JOIN (
SELECT date_start AS boundary
FROM bookings
WHERE date_start BETWEEN '2016-01-17 00:00:00' AND '2016-01-17 23:59:59'

UNION
SELECT date_end
FROM bookings
WHERE date_end BETWEEN '2016-01-17 00:00:00' AND '2016-01-17 23:59:59'

UNION
SELECT MAX(boundary)
FROM (
SELECT MAX(date_start) AS boundary
FROM bookings
WHERE date_start <= '2016-01-17 00:00:00'

UNION ALL
SELECT MAX(date_end)
FROM bookings
WHERE date_end <= '2016-01-17 23:59:59'

) t
) t ON date_start <= boundary AND boundary < date_end
LEFT OUTER JOIN cart ON cart_bookings = id
GROUP BY boundary
) t

最佳答案

哇,好吧,这就是答案。原著显然并不完整。它需要包括所请求的时间范围的开始/结束日期。

  UNION
SELECT :date_start
UNION
SELECT :date_end

完整代码:

SELECT
MAX(simultaneous) AS max_count,
MIN(simultaneous) AS min_count
FROM (
SELECT IFNULL(SUM(
(
CASE WHEN (
listings = '2f23f23f'
AND
(state = 'active')
)
THEN count
ELSE 0
END
)
),0) AS simultaneous
FROM bookings RIGHT JOIN (
SELECT date_start AS boundary
FROM bookings
WHERE date_start BETWEEN '2016-01-17 00:00:00' AND '2016-01-17 23:59:59'

UNION
SELECT date_end
FROM bookings
WHERE date_end BETWEEN '2016-01-17 00:00:00' AND '2016-01-17 23:59:59'

UNION
SELECT MAX(boundary)
FROM (
SELECT MAX(date_start) AS boundary
FROM bookings
WHERE date_start <= '2016-01-17 00:00:00'

UNION ALL
SELECT MAX(date_end)
FROM bookings
WHERE date_end <= '2016-01-17 23:59:59'

) t
UNION
SELECT :date_start
UNION
SELECT :date_end
) t ON date_start <= boundary AND boundary < date_end
LEFT OUTER JOIN cart ON cart_bookings = id
GROUP BY boundary
) t

关于mysql - 重叠预订的最小和最大计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34781742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com