SQL查询:不使用子查询

时间:2010-01-12 05:56:49

标签: sql-server sql-server-2005

如何在不使用子查询的情况下从第一个表中获取第一个表中的所有记录? 我想使用Join ...

2 个答案:

答案 0 :(得分:2)

SELECT A.someColumn
FROM A LEFT JOIN B
ON A.ID = B.ID
WHERE B.ID IS NULL

答案 1 :(得分:2)

您甚至可以使用

a)EXCEPT

b)Where Not in

e.g。   示例数据

declare @t1 table(id1 int, recordsA varchar(20))
insert into @t1 
select 1,'record1' union all 
select 2,'record2' union all
select 3,'record3' union all
select 4,'record4' union all
select 5,'record5'

declare @t2 table(id2 int, recordsB varchar(20))
insert into @t2 
select 1,'record1' union all 
select 2,'record2' union all
select 3,'record3' 

<强>查询:1

select t1.id1,t1.recordsA from @t1  t1
except
select t2.id2,t2.recordsB from @t2 t2

查询2:

select t1.id1,t1.recordsA from @t1  t1 
where  t1.recordsA not in(select t2.recordsB from @t2 t2)

<强>输出:

id1 recordsA
4   record4
5   record5