我想创建 sql req 以在同一列中两次使用 order by 我尝试使用此示例
SELECT titleEN, smallDescriptionEN, StartDate, EndDate
FROM event
WHERE EndDate > NOW()
ORDER BY StartDate ASC
UNION
SELECT titleEN, smallDescriptionEN, StartDate, EndDate
FROM event
WHERE EndDate < NOW()
ORDER BY StartDate DESC
但是sql错误表明UNION和ORDER BY的使用不正确
#1221 - Incorrect usage of UNION and ORDER BY
最佳答案
末尾的一个 Order By
足以对完整的结果集进行排序
SELECT titleEN,
smallDescriptionEN,
StartDate,
EndDate
FROM event
WHERE EndDate < Now()
UNION
SELECT titleEN,
smallDescriptionEN,
StartDate,
EndDate
FROM event
WHERE EndDate > Now()
ORDER BY StartDate DESC;
关于mysql - 如何在同一列中创建2个order by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31288003/