I want to sort my results based on two varchar columns COL_A
and COL_B
.
我想基于两个varchar列COL_A和COL_B对结果进行排序。
If the value of COL_A
is ('val1', 'val2', 'val3')
, I want it the appear at the top, otherwise I want it to appear at the bottom. Then, I want to do a simple lexicographical sort on COL_B. In MySQL I would have done it like this:
如果COL_A的值是('val1'、'val2'、'val3'),我希望它出现在顶部,否则我希望它出现在底部。然后,我想在COL_B上做一个简单的字典式排序。在MySQL中,我会这样做:
ORDER BY IF(COL_A IN ('val1', 'val2', 'val3'), 0, 1), COL_B
But this doesn't work in SQL Server 2008. Is there an equivalent?
但是这在SQL Server 2008中是行不通的。有一个等价的吗?
5
Use case when
instead of IF in the query which you tried.
用例,而不是在您尝试的查询中。
ORDER BY (Case When COL_A IN ('val1', 'val2', 'val3') Then 0 Else 1 End), COL_B
Put this in place of query which you tried.
把它放在您尝试过的查询位置。
MSDN:
MSDN:
http://msdn.microsoft.com/en-IN/library/ms181765.aspx
http://msdn.microsoft.com/en-IN/library/ms181765.aspx
Syntax And Example:
语法和例子:
http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/
Tutorial Might Helpful:
教程中可能会有帮助:
http://www.tizag.com/sqlTutorial/sqlcase.php
http://www.tizag.com/sqlTutorial/sqlcase.php
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/04/23/490ec347a48cf4f4d9a8a7a3057ed54e.html。