所以我有一个看起来像这样的查询:
SELECT IncidentNumber,ToiletType,ToiletDangers,IncidentDate FROM Core.LostLawsuits
…返回以下结果:
+----------------+------------+---------------------------+---------------+ | IncidentNumber | ToiletType | ToiletDangers | Incident Date | +----------------+------------+---------------------------+---------------+ | 2100 | A | Spontaneous Combustion | 2/1/2016 | +----------------+------------+---------------------------+---------------+ | 2101 | P | Attracts Bear Stampede | 2/1/2016 | +----------------+------------+---------------------------+---------------+
我想做的是获取结果,但在输出时更改ToiletType列结果.目前,我正在使用嵌套的REPLACE(),并想知道是否有更好/不同的方法,同时维护ToiletType的一列结果:
SELECT IncidentNumber,REPLACE(REPLACE(ToiletType,'A','Automatic Standard'),'P','Portable Potty') as ToiletType,IncidentDate FROM Core.LostLawsuits
解决方法
CASE表达式适用于您的示例:
case ToiletType when 'A' then 'Automatic Standard' when 'P' then 'Portable Potty' end
即
SELECT IncidentNumber,case ToiletType when 'A' then 'Automatic Standard' when 'P' then 'Portable Potty' end as ToiletType,IncidentDate FROM Core.LostLawsuits
也许更好的是加入ToiletTypes表?