如果id是数字型select case when flag=0 then cast(id as varchar(100)) when flag=1 then name end as name,age from tablename
如果一个表里有四个列,如:ID,Name,Age,Flag, 能不能在查询的时候判断Flag的值如过是0就输出Id,Age的值, 如果是1就返回Name,Age的值? ----------------------------------------------------- select case flag when 0 then ID else Name as col1, Age, Flag from tablename
问题解决啦,谢谢两位!! :) i9988(冒牌j9988 V0.2) 老师您的这个as那提示错误啊?现在这样如果可以,是重命名两个列;要是只把其中一列重新命名怎么写啊?select case when flag=0 then id when flag=1 then name end as name,age from tablename
select replace(replace(Flag,0,[ID]),1,[name]) as col1 ,age
from tablename
能不能在查询的时候判断Flag的值如过是0就输出Id,Age的值,
如果是1就返回Name,Age的值?
-----------------------------------------------------
select
case flag when 0 then ID else Name as col1,
Age,
Flag
from tablename
i9988(冒牌j9988 V0.2) 老师您的这个as那提示错误啊?现在这样如果可以,是重命名两个列;要是只把其中一列重新命名怎么写啊?select case when flag=0 then id when flag=1 then name end as name,age
from tablename
这个方法很巧!