TSQL - 是否可以定义排序顺序? [英] TSQL - Is it possible to define the sort order?

查看:30
本文介绍了TSQL - 是否可以定义排序顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为返回的结果定义排序顺序?

Is it possible to define a sort order for the returned results?

我希望排序顺序是橙色"苹果"草莓",而不是升序或降序.

I would like the sort order to be 'orange' 'apple' 'strawberry' not ascending or descending.

我知道 ORDER BY 可以做 ASC 或 DESC,但是有 DEFINED('orange', 'apple', 'strawberry') 类型的东西吗?

I know ORDER BY can do ASC or DESC but is there a DEFINED('orange', 'apple', 'strawberry') type thing?

这将在 SQL Server 2000 上运行.

This will be running on SQL Server 2000.

推荐答案

它非常笨拙,但您可以使用 CASE 语句进行排序:

It's incredibly clunky, but you can use a CASE statement for ordering:

SELECT * FROM Blah 
ORDER BY CASE MyColumn 
    WHEN 'orange' THEN 1 
    WHEN 'apple' THEN 2 
    WHEN 'strawberry' THEN 3 
    END 

或者,您可以创建一个包含排序字段和排序顺序的辅助表.

Alternately, you can create a secondary table which contains the sort field and a sort order.

TargetValue  SortOrder
orange       1
apple        2
strawberry   3

然后将您的桌子连接到这张新桌子上.

And join your table onto this new table.

这篇关于TSQL - 是否可以定义排序顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆