SQL Server,相當於這個MySQL排序子句

[英]SQL Server equivalent of this MySQL sort clause


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中是行不通的。有一個等價的嗎?

1 个解决方案

#1


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/

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



 
  © 2014-2022 ITdaan.com 联系我们: