[英]SQL Select Specific Records based on Text Value
我有一張國家表...
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個國家,但不允許我在它們下面顯示另一個國家。
您不需要UNION即可得到結果,可以在ORDER BY中使用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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.