select a.字段1,b.字段2 from tb2 a left join tb1 b on a.字段1 = b.字段1
select a.* from a where exists(select 1 from a where a.col1=b.col1)
我想达到这样的结果: 字段1 字段2 1 a 2 b 3 c 4 5你的查询后面二行无法显示出来
select a.字段1,isnull(b.字段2,'') 字段2 from tb2 a left join tb1 b on a.字段1 = b.字段1
select t2.* , t1.* from t2 left join t1 on t2.字段1 = t2.字段1 select t2.* , isnull(t1.字段2,'') 字段2 from t2 left join t1 on t2.字段1 = t2.字段1
忘记说,表2中的记录会有这样重复的。 字段1 字段2 1 a 2 a 3 a 1 b 2 b 3 b
用上面表的字段2做为查询条件,与下面这个表一起查询, 字段1 1 2 3 4 5 如果查询条件字段2为a,则结果为: 字段1 字段2 1 a 2 a 3 a 4 5
select a.字段1,b.字段2 from 表2 a left join 表1 b on a.字段1 = b.字段1 显示结果为: 字段1 字段2 1 a 2 b 3 c 4 5
前面没描述清楚,是这样的:表1 字段1 字段2 1 a 2 a 3 a 1 b 2 b 3 b 表2 字段 1 2 3 4 5以表1的字段为查查询条件,如查询条件字段2为a,则结果为: 字段1 字段2 1 a 2 a 3 a 4 5
select A.字段1,B.字段2 from 表2 A left join 表1 on 表2.字段 = 表1.字段1 where B.字段2 = 'a'
select tb2.*,tb1.col from tb2 left join tb1 on tb1.col1=tb2.col1 and tb1.col1='a'
--> 测试数据: @表1 declare @表1 table (字段1 int,字段2 varchar(1)) insert into @表1 select 1,'a' union all select 2,'a' union all select 3,'a' union all select 1,'b' union all select 2,'b' union all select 3,'b'--> 测试数据: @表2 declare @表2 table (字段 int) insert into @表2 select 1 union all select 2 union all select 3 union all select 4 union all select 5select a.*,b.字段2 from @表2 a left join @表1 b on a.字段=b.字段1 and b.字段2='a' /* 字段 字段2 ----------- ---- 1 a 2 a 3 a 4 NULL 5 NULL */这个是重复贴?
select a.字段1,b.字段2
from tb2 a left join tb1 b on a.字段1 = b.字段1
from a where exists(select 1 from a where a.col1=b.col1)
字段1 字段2
1 a
2 b
3 c
4
5你的查询后面二行无法显示出来
select a.字段1,isnull(b.字段2,'') 字段2
from tb2 a left join tb1 b on a.字段1 = b.字段1
from t2 left join t1
on t2.字段1 = t2.字段1 select t2.* , isnull(t1.字段2,'') 字段2
from t2 left join t1
on t2.字段1 = t2.字段1
字段1 字段2
1 a
2 a
3 a
1 b
2 b
3 b
用上面表的字段2做为查询条件,与下面这个表一起查询,
字段1
1
2
3
4
5
如果查询条件字段2为a,则结果为:
字段1 字段2
1 a
2 a
3 a
4
5
from 表2 a left join 表1 b on a.字段1 = b.字段1
显示结果为:
字段1 字段2
1 a
2 b
3 c
4
5
字段1 字段2
1 a
2 a
3 a
1 b
2 b
3 b
表2
字段
1
2
3
4
5以表1的字段为查查询条件,如查询条件字段2为a,则结果为:
字段1 字段2
1 a
2 a
3 a
4
5
from 表2 A left join 表1
on 表2.字段 = 表1.字段1
where B.字段2 = 'a'
from tb2
left join tb1 on tb1.col1=tb2.col1 and tb1.col1='a'
--> 测试数据: @表1
declare @表1 table (字段1 int,字段2 varchar(1))
insert into @表1
select 1,'a' union all
select 2,'a' union all
select 3,'a' union all
select 1,'b' union all
select 2,'b' union all
select 3,'b'--> 测试数据: @表2
declare @表2 table (字段 int)
insert into @表2
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5select a.*,b.字段2 from @表2 a left join @表1 b on a.字段=b.字段1 and b.字段2='a'
/*
字段 字段2
----------- ----
1 a
2 a
3 a
4 NULL
5 NULL
*/这个是重复贴?