我想算一下客人住在一个范围内的夜晚数。所以不到7晚,7至14晚,最后超过14晚。
我希望输出看起来像这样:
Range | Count
----------------------
<= 7 Nights | 3
8 - 14 Nights | 2
15 Nights + | 1
我正在使用MYSQL并尝试了以下两种解决方案,但似乎没有出现任何结果。
SELECT
CASE
WHEN booking_num_nights <= 7 THEN '<= 7 Nights'
WHEN booking_num_nights > 7 and booking_num_nights <= 14 THEN '8 - 14 Nights'
WHEN booking_num_nights > 14 THEN '15 Nights +'
END AS range, count(*) AS count_num
FROM BOOKING
WHERE booking_property_id = :id
GROUP BY range
另一次尝试如下:
SELECT booking_num_nights as range, count(*) as count_num
FROM (SELECT
CASE
WHEN booking_num_nights <= 7 THEN '<= 7 Nights'
WHEN booking_num_nights > 7 and booking_num_nights <= 14 THEN '8 - 14 Nights'
WHEN booking_num_nights > 14 THEN '15 Nights +'
END AS range
FROM BOOKING)
WHERE booking_property_id = :id
GROUP BY range
答案 0 :(得分:0)
range
是reserved word。要么使用不同的名称,要么逃避它:
SELECT
CASE
WHEN booking_num_nights <= 7 THEN '<= 7 Nights'
WHEN booking_num_nights > 7 and booking_num_nights <= 14 THEN '8 - 14 Nights'
WHEN booking_num_nights > 14 THEN '15 Nights +'
END AS `range`, count(*) AS count_num
FROM BOOKING
WHERE booking_property_id = :id
GROUP BY `range`