SQL三表联合查询问题,求指导!
表A
AId AName
1 aa
2 bb
3 cc
表B
BId BName
1 aa
2 bb
3 cc
表C
AId BId
1 1
1 2
我想通过表C 条件为AId,排除Aid为1的剩下的表B的信息,求Sql大神们指教谢谢!
查询结果表
AId AName BId BName
1 aa 3 cc
Sql
[解决办法]
aid为1的话c表就没数据了哦
[解决办法]
select AId AName BId BName from A,B,C where A.AId=C.AId and C.BId=B.BId and C.AId<>1
[解决办法]
create table a(AId int,AName varchar(10))
insert into a
select 1, 'aa' union all
select 2, 'bb' union all
select 3, 'cc'
create table B(BId int,BName varchar(10))
insert into b
select 1 ,'aa' union all
select 2 ,'bb' union all
select 3 ,'cc'
create table C(AId int,BId int)
insert into c
select 1,1 union all
select 1,2
select *
from
(
select *
from a
where a.aid = 1
)a
cross join
(
select b.*
from b
where b.bid not in (select bid from c where aid = 1)
)b
/*
AIdANameBIdBName
1aa 3cc
*/