0
点赞
收藏
分享

微信扫一扫

分享sql语句改造增加判断取值case when then过程记录

需求描述:

1.单点登陆时,总部人员使用个人账号,分部人员使用公共账号。

2.原语句:select workcode from hrmresource where id=?

需求分析:

1.个人账号:select workcode  from hrmresource where id=?

2.公共账号:select LXFILEZZH from  hrmsubcompanydefined where subcomid=

3.总部ID:22

4.当前分部:select subcompanyid1 from hrmresource where id=?


SQL输出:

1.取当前操作者人员ID:225 为例,返回workcode,subcompanyid1。

 select workcode,subcompanyid1 from hrmresource where id=225


2.判断当前分部 是否是22 如果是则取workcode否则返回11.

  select case subcompanyid1 when 22 then workcode else 11 end as workcode  from hrmresource where id=225


3.语句涉及多表前给别称。

  (1)select case h.subcompanyid1 when 22 then h.workcode else 11 end as workcode  from hrmresource h where h.id=225

  (2)select s.LXFILEZZH from  hrmsubcompanydefined s where s.subcomid=

4.语句组合

 select case h.subcompanyid1 when 22 then h.workcode else (select s.LXFILEZZH from  hrmsubcompanydefined s where s.subcomid=h.subcompanyid1) end as workcode  from hrmresource h where h.id=225



备注:输出过程,每一点点修改都可以做查询测试,测试有问题就及时处理。

举报

相关推荐

0 条评论