求一存储过程.两表关联
有一表如下:
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