当前位置: 代码迷 >> Sql Server >> 求一存储过程.两表关联,该如何处理
  详细解决方案

求一存储过程.两表关联,该如何处理

热度:62   发布时间:2016-04-27 16:39:31.0
求一存储过程.两表关联
有一表如下:
ID   CodeName
1     a
2     c
3     d

另一表如下:
ID     UserName
1         bb
2         bb
1         cc
3         cc
2         dd

希望能根据UserName得到如下表:
如果UserName=bb
ID       CodeName       IsIn
1               a               true  
2               c               true
3               d               false
如果UserName=cc
ID       CodeName       IsIn
1               a               true  
2               c               false
3               d               true




------解决方案--------------------
select A.ID, A.CodeName,case when B.UserName is null then 'False ' else 'True ' end As IsIn
from 表1 as A
left join (select * from 表2 where UserName= 'bb ') AS B on A.id=B.id
  相关解决方案