日期:2014-05-18 浏览次数:20580 次
--> 测试数据: @用户表
declare @用户表 table (id int,用户名 varchar(4),标识 int)
insert into @用户表
select 1,'张三',1 union all
select 2,'李四',1 union all
select 3,'王五',2
--> 测试数据: @信息表
declare @信息表 table (id int,用户id int,用户名 varchar(4),性别 varchar(2))
insert into @信息表
select 1,1,'张三','男' union all
select 2,2,'李四','男' union all
select 3,3,'王五','女'
--第一种思路
select
b.用户id,b.用户名,b.性别
from @用户表 a left join @信息表 b on a.id=b.用户id
where a.标识=1
--第二种思路
select
b.用户id,b.用户名,b.性别
from @信息表 b
where (select 标识 from @用户表 a where a.id=b.用户id)=1
/*
用户id 用户名 性别
----------- ---- ----
1 张三 男
2 李四 男
*/
------解决方案--------------------
select
b.用户id,b.用户名,b.性别
from @用户表 a left join @信息表 b on a.id=b.用户id
where a.标识=1