我正在为图书馆开发图书预订系统。 在预订表中有以下字段: id_reservation,id_user,id_book,datetime。
我想计算早上完成的预订和晚上预订,假设早上10:00至15:00,晚上15:00至21:00。
我需要这样的东西: 通过 time_period
从预订组中选择count(id_reservation)作为total_reservations任何帮助如何区分两个时间段以便在 GROUP BY 中使用它们?
非常感谢!!
答案 0 :(得分:3)
试试这个:
SELECT (CASE WHEN TIME(datetime) BETWEEN '10:00:00' AND '15:00:00' THEN 'Morning'
WHEN TIME(datetime) BETWEEN '15:00:00' AND '21:00:00' THEN 'Evening'
ELSE 'NA'
END) AS time_period,
COUNT(id_reservation) AS total_reservations
FROM reservations
GROUP BY time_period;