在SELECT JOIN语句中显示基于Int Value的字符串

时间:2016-09-16 11:34:24

标签: sql

我有一个select语句,我想根据它的值转换为字符串,即

我想要的是:

SELECT SchoolID (IF SchoolID = 1 Convert STRING("London"), IF SchoolID = 2 Convert STRING ("Glasglow") ELSE Convert STRING(new school")), 
L.Location
FROM Schools SC
Join Location L
On L.LocationID = SC.LocationID 
Where SchoolID < 200

不确定如何处理此事。

非常感谢, 萨姆

- 编辑括号是我应该查看的语法而不是实际代码

3 个答案:

答案 0 :(得分:1)

您可以使用CASE这样的表达式:

SELECT SchoolID
, case SchoolID
    when 1 then 'London'
    when 2 then 'Glasglow'
    else        'new school' 
  end as SchoolName
, L.Location
FROM Schools SC
Join Location L
On L.LocationID = SC.LocationID 
Where SchoolID < 200

答案 1 :(得分:1)

SELECT SchoolID, CASE SchoolID 
WHEN 1 THEN 'London' 
WHEN 2 THEN 'Glasglow'
ELSE 'new school'
END AS New_Value , 
L.Location
FROM Schools SC
Join Location L
On L.LocationID = SC.LocationID 
Where SchoolID < 200

答案 2 :(得分:1)

案例陈述也可以这样写:

SELECT SchoolID, 
       CASE WHEN SchoolID = 1 THEN 'London'
       WHEN SchoolID = 2 THEN 'Glasglow'
       ELSE 'new school'
       END AS city , 
       L.Location
FROM Schools SC
JOIN LOCATIONL ON L.LocationID = SC.LocationID 
WHERE SchoolID < 200