如下表
A ida name
11 A1
12 A2
13 A3
14 A4B idb name
21 b1
22 b2
23 b3
24 b4C ida idb
11 21
11 22
12 21
12 b3 我想查出来是这样子的查出 11 在B中选中的 (就是要显示 B 里所有的记录,但不重复,还能标出哪个记录是 11 有的)
11 21 b1 选中
11 22 b2 选中
11 23 b3 没选中
11 24 b4 没选中
A ida name
11 A1
12 A2
13 A3
14 A4B idb name
21 b1
22 b2
23 b3
24 b4C ida idb
11 21
11 22
12 21
12 b3 我想查出来是这样子的查出 11 在B中选中的 (就是要显示 B 里所有的记录,但不重复,还能标出哪个记录是 11 有的)
11 21 b1 选中
11 22 b2 选中
11 23 b3 没选中
11 24 b4 没选中
from a cross join b left join c on a.ida=c.ida and b.idb=c.idb
where ....
if object_ID('ta') IS NOT NULL DROP TABLE ta
go
create table ta(ida int,name varchar(11) )
go
insert ta select
11, 'A1'union all select
12, 'A2'union all select
13, 'A3' union all select
14, 'A4' if object_ID('tb') IS NOT NULL DROP TABLE tb
go
create table tb(idb int,name varchar(11))
go
insert tb select
21 , 'b1' union all select
22 , 'b2' union all select
23 , 'b3' union all select
24 , 'b4' if object_ID('tc') IS NOT NULL DROP TABLE tc
go
create table tc(ida int,idb int)
go
insert tc select
11 , 21 union all select
11 , 22 union all select
12 , 23 union all select ---? 这2条语句idb是不是有问题??
12 , 24 select 11 as id,b.idb,b.name ,zt=case when c.ida=11 then '选中' else '没选中' end
from tb b
left join tc c on b.idb=c.idb
--left join ta a on a.ida=c.idaid idb name zt
----------- ----------- ----------- ------
11 21 b1 选中
11 22 b2 选中
11 23 b3 没选中
11 24 b4 没选中(4 行受影响)