sql如何巧妙用case语句做输出目标结果--蠢萌蠢萌

来源:互联网 发布:淘宝网平板电脑版下载 编辑:程序博客网 时间:2024/06/03 22:45

现象:

     数据库中

     select DISTINCT fd_cost_code,CASE fd_cost_code

    FROM ekp_costcenter_dubget
     where fd_cost_code='SC001';

要求:

     当是在前端选中了fd_cost_code等于SCC01,且SCC01数据库中对应的只有一行或多行,SZO和JNO也是对应的只有一行或多行;要查出fd_cost_code='SCC01' or fd_cost_code='JNO' or fd_cost_code='SZO';

     同时当在前端选中了fd_cost_code等于SZO,不能查出其他结果,查询结果为空或者其他;用子查询,union all和or或者and没法实现;


方法:

select DISTINCT fd_cost_code,CASE fd_cost_code
WHEN 'SCC01' THEN 'SZO'
ELSE '其他' END
AS fd_cost_code_2
FROM ekp_costcenter_dubget
where fd_cost_code='SC001';


select DISTINCT fd_cost_code,CASE fd_cost_code
WHEN 'SCC01' THEN 'JNO'
ELSE '其他' END
AS fd_cost_code_3
FROM ekp_costcenter_dubget
where fd_cost_code='SC001';

原创粉丝点击