我有一个小问题.我正在使用SQL数据库的结果填充选择标记.我想要的是最后一个作为顶部条目添加,其余按字母顺序排序.
所以会回来:
*----------*-------------* developerID|developerName *----------*-------------* | 40 | ZZZ Dev | | 39 | A Dev | | 38 | Be New Dev | *----------*-------------*
目前它只是按降序选择所有条目:
"SELECT developerName, developerID FROM developer ORDER BY developerID DESC"
哪个好,但不如我想的那么可用.
使用纯MySQL是我想要的吗?
假设定义添加开发人员的列是developerAdded(某种时间戳):
SELECT developerName, developerID, 1 AS ordering FROM developer WHERE developerAdded = (SELECT MAX(developerAdded) FROM developer) UNION SELECT developerName, developerID, 2 AS ordering FROM developer WHERE developerAdded != (SELECT MAX(developerAdded) FROM developer) ORDER BY ordering, developername;
如果还有其他一些神奇的方法来确定最近添加的开发人员(例如,最大的开发者ID),请相应地调整子查询.
请注意,即使多个开发人员满足"最近添加的"标准,此查询也能正常工作; 它们出现在按字母顺序排列的开发人员之前.
由于最近添加的开发人员具有最大的开发者ID,因此修订后的查询应为:
SELECT developerName, developerID, 1 AS ordering FROM developer WHERE developerID = (SELECT MAX(developerID) FROM developer) UNION SELECT developerName, developerID, 2 AS ordering FROM developer WHERE developerID != (SELECT MAX(developerID) FROM developer) ORDER BY ordering, developername;