在Sql Server中获得所需结果的案例 [英] Case in Sql Server for getting the desired results

查看:65
本文介绍了在Sql Server中获得所需结果的案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张这样的桌子:

ctrycode国家/地区名称费用
英国英国2.3
uk ch x 3.6

在编写查询期间,我需要作为输出来获取 ch x 的国家/地区名称,例如,

从表名中选择*

结果应该是这样的
英国英国2.3
英国英国3.6

我所做的就是这样:

Hi, I have a table like this:

ctrycode countryname fees
uk united kingdom 2.3
uk ch x 3.6

During writing a query what I need as output is to get the countryname for ch x as united kingdom for example

select * from tablename

result should be like this
uk united kingdom 2.3
uk united kingdom 3.6

What I did is like this:

select * from tablname 
where countryname= case when countryname=''ch x'' then ''United Kingdom''
end 



如果执行上述操作,则不会得到该特定行.有人可以帮我这个忙吗?
我正在学习这一内容,如果我不清楚,请通知我.



I am not getting that particular row if I do as the above. Can someone help me out with this?
I am learning on this one, please let me know if I am not clear.

推荐答案

您需要问自己为什么拥有"ch x"如果是英国,则使用国家/地区名称.

无论如何...在SELECT语句中执行case语句

You need to ask yourself why you''ve got ''ch x'' in country name if it''s supposed to be United Kingdom.

Anyway...do the case statement in your SELECT statement

select 
    ctrycode, case when countryname='ch x' then 'United Kingdom' ELSE countryname end AS countryname, fees 
from 
    tablname


这篇关于在Sql Server中获得所需结果的案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆