表中有A、B两个字段,现求达到以下功能的效率最高的语句
表中有A、B两个字段,现求达到以下功能的效率最高的语句:
若A的值为 'FTG '则获得B字段值;若B的值为 'FTG '则获得A字段值
[解决办法]
select case when a = 'ftg ' then b when b = 'ftg ' then a end as xx from tablename
[解决办法]
select case when A= 'FTG ' THEN B ELSE A END ,CASE WHEN B= 'FTG ' THEN A ELSE B END
from tab
[解决办法]
select b from tab
when A= 'FTG '
union all
select a from tab
where B= 'FTG '
[解决办法]
union all有可能产生重复记录,建议用union
select id,b, 'b ' as 'field ' from tab
when A= 'FTG '
union
select id,a, 'a ' from tab
where B= 'FTG '