首先列出特定值

时间:2014-05-21 16:57:43

标签: sql sql-server database sql-server-2005

我到处搜索这个但是找不到任何解决方案,我需要在下面的sql语句中首先列出一个像'New York'这样的特定城市,我试着放置           按案例命令City.name_en           当'纽约'那么1           ELSE 2 END,City.name_en 在内部的sql语句中但是没有用,有没有解决方法呢?

SELECT * FROM (
SELECT RTRIM(Organization.name_en) as '@name','flag.png'  AS  '@flag',
    RTRIM(Country.name_en) as 'country',RTRIM(city.name_en) as 'city',Organization.id as 'OrganizationID',
    ROW_NUMBER() OVER (ORDER BY sequence_num) AS RowNum
FROM Organization 
LEFT JOIN City ON City.id = city_id
LEFT JOIN Country ON country.id = Organization.country_id
LEFT JOIN Industry ON Industry.id = Industry_id
WHERE 
 (industry_id =@industry_id OR Coalesce(@industry_id,'') = '')
AND (Organization.name_en LIKE '%' + @OrganizationName + '%' OR Coalesce(@OrganizationName,'') = '')
) AS SOD

WHERE    SOD.RowNum BETWEEN ((@PageNumber-1)*@RowsPerPage)+1
AND @RowsPerPage*(@PageNumber)

FOR XML PATH('organization'), ROOT('organizations')

3 个答案:

答案 0 :(得分:0)

使用ORDER BY FIELD
例如: -

ORDER BY FIELD(City.name_en, 'New York', 'CA')

它会给你预期的结果。

答案 1 :(得分:0)

使用Union

示例

从country_name ='New York'

的国家/地区选择country_code,country_name

联合所有

从country_name!='New York'的国家/地区选择country_code,country_name 按country_name排序

答案 2 :(得分:0)

其实我这样解决了:

ROW_NUMBER() OVER (ORDER BY CASE city.name_en
           WHEN @cityName THEN 1 
           ELSE 2 END ,city.name_en) AS RowNum