请有人解释为什么排序工作?
以下是代码示例:
SET @orderBy = 0;
SET @isDescending = 1;
DROP TEMPORARY TABLE IF EXISTS orders;
CREATE TEMPORARY TABLE orders (ID BIGINT NOT NULL PRIMARY KEY, CreationDate DATETIME NOT NULL, CustomerID BIGINT NOT NULL);
INSERT INTO orders VALUES(1, '2016/08/28', 2);
INSERT INTO orders VALUES(2, '2016/08/26', 2);
INSERT INTO orders VALUES(3, '2016/08/27', 2);
SELECT * FROM orders WHERE CustomerID = 2
ORDER BY
CASE WHEN @orderBy = 0 AND @isDescending = 0 THEN
CreationDate
END ASC,
CASE WHEN @orderBy = 0 AND @isDescending = 1 THEN
CreationDate
END DESC;
DROP TEMPORARY TABLE IF EXISTS orders;
参见mysqlworkbench screenshot:
我在SQL代码中看不到任何错误。我使用5.7.12和5.7.14版本。排序行为是随机的。
感谢您的回复。