我想创建一个查询,在两个日期的手上对结果进行排序。 这是一个switch语句。但这似乎不起作用。
简化版本下方(未指定字段)
SELECT *
FROM `system_booking`
JOIN `system_address` ON `system_address`.`address_id` = `system_booking`.`booking_address`
JOIN `system_bookingprice` ON `system_booking`.`booking_id` = `system_bookingprice`.`bookingprice_link`
JOIN `system_vehicle` ON `system_vehicle`.`vehicle_id` = `system_booking`.`booking_vehicle`
JOIN `system_parkingfee` ON `system_booking`.`booking_id` = `system_parkingfee`.`parkingfee_link`
WHERE (`booking_start` LIKE ? OR `booking_end` LIKE ?)
ORDER BY (
CASE WHEN `booking_start` LIKE ? THEN `booking_start` ASC
CASE WHEN `booking_end` LIKE ? THEN `booking_end` ASC
END)
然而,这会产生MySQL错误。
您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在booking_end
LIKE' 2015-06-01%'之后使用ASC CASE附近使用正确的语法。那么booking_end
A'第9行
我希望有人能告诉我我做错了什么。
答案 0 :(得分:2)
每个END
都需要CASE
(还有逗号):
ORDER BY (CASE WHEN `booking_start` LIKE ? THEN `booking_start` END) ASC,
(CASE WHEN `booking_end` LIKE ? THEN `booking_end` END) ASC
答案 1 :(得分:0)
@Gordon Linoff
那很有效!!!
SELECT *
FROM `system_booking`
JOIN `system_address` ON `system_address`.`address_id` = `system_booking`.`booking_address`
JOIN `system_bookingprice` ON `system_booking`.`booking_id` = `system_bookingprice`.`bookingprice_link`
JOIN `system_vehicle` ON `system_vehicle`.`vehicle_id` = `system_booking`.`booking_vehicle`
JOIN `system_parkingfee` ON `system_booking`.`booking_id` = `system_parkingfee`.`parkingfee_link`
WHERE (`booking_start` LIKE ? OR `booking_end` LIKE ?)
ORDER BY
(CASE WHEN `booking_start` LIKE ? THEN `booking_start` END) ASC,
(CASE WHEN `booking_end` LIKE ? THEN `booking_end` END) ASC
谢谢!