我试图通过2个类似的事情来查询结果
通过“reservationStatus”然后取决于两个字段之一,具体取决于reservationStatus。
这是我的mysql代码
SELECT rs.reservationStatus,
DATE_FORMAT(rs.dateFrom, "%d-%m-%Y %h:%i %p") AS pickupTime,
DATE_FORMAT(rs.dateTo, "%d-%m-%Y %h:%i %p") AS dropoffTime,
cu.fullName,
ve.name FROM reservation AS rs
INNER JOIN customers AS cu ON rs.CustomerID = cu.customerID
INNER JOIN vehicles AS ve ON rs.Vehicle_id = ve.Vehicles_id
WHERE rs.dateFrom <= DATE_ADD(curdate() , INTERVAL 86399 SECOND) AND rs.reservationStatus IN (1,2)
ORDER BY rs.reservationStatus, CASE WHEN rs.reservationStatus = 1 THEN rs.dateFrom ELSE rs.dateTo END ASC
这给了我一个语法错误。但是,如果我删除了rs.reservationStatus,并在那里保留了案例陈述,那么它就会运行,但我没有看到按照它应该排序的结果。
我需要的是像这样对结果进行排序
如何让上面的查询正确运行?
答案 0 :(得分:0)
“CASE”表达式根据从其他列或列内容派生的表达式生成一些列结果。试试吧
SELECT
column1,
CASE WHEN ... THEN ... ELSE ... AS customcolumnname,
column2.sometable,
column3 ..
FROM
mytable
ORDER BY
customcolumnname ASC,
column1 DESC
或类似。