SELECT * FROM TableName WHERE ORDER BY CASE @OrderByColumn WHEN 1 THEN Forename WHEN 2 THEN Surname END;
我有一条类似于上面的语句,它使我可以动态选择如何对查询结果进行排序。但是,如何指定要订购DESC的姓氏和姓氏ASC?
DESC
ASC
您需要将您的内容ORDER BY分为两部分:
SELECT * FROM TableName WHERE ORDER BY (CASE @OrderByColumn WHEN 1 THEN Forename END) DESC -- Forename --> descending , (CASE @OrderByColumn WHEN 2 THEN Surname END) ASC -- Surname --> ascending