2013-10-11 32 views
0

相關記錄我的國家的一個表....SQL SELECT基於文本價值

CountryID  CountryCode CountryName 
1    AF    Afghanistan 
2    AX    ALAND ISLANDS 
3    AL    Albania 
4    DZ    Algeria etc. 

我將用它來填充一個下拉菜單在網頁上。我希望能夠讓以下四個國家先出現,然後讓整個名單以ASC順序出現在這四個下面。

CountryID CountryCode  CountryName 
236   US    United States 
40    CA    Canada 
76    FR    France 
235   UK    United Kingdom 

我已經試過各種方法,但沒有得到它。

SELECT * 
From [dbo].[tblCountries] 
WHERE CountryID IN (236,40,76,235) 
ORDER BY CountryName asc 

這給了我想要的4個國家,但不允許我在其下面顯示另一個國家。

回答

0

你不需要UNION得到結果,你可以在ORDER使用CASE表達:

select [CountryID], [CountryCode], [CountryName] 
from tblCountries 
order by 
    case [CountryID] 
    when 236 then 1 
    when 40 then 2 
    when 76 then 3 
    when 235 then 4 
    else 5 
    end, [CountryName]; 

SQL Fiddle with Demo