日期:2014-05-17  浏览次数:20439 次

一个简单的SQL请教
Ta 表
A1 A2 A3
01 02 03
01 03 04
02 05 08



Tb表
B1 B2 B3
01 02 03
01 07 09
02 04 05

我想得到Tb表中在Ta表中不存在的记录
得到的结果
B1 B2 B3
01 07 09
02 04 05



麻烦各位大虾。。。。。

------解决方案--------------------
SQL code

IF OBJECT_ID('Ta','U') IS NOT NULL 
DROP TABLE Ta

CREATE TABLE Ta(A1 varchar(4),A2 varchar(4),A3 varchar(4))

INSERT INTO Ta(A1,A2,A3)
SELECT '01','02','03' UNION ALL 
SELECT '01','03','04' UNION ALL
SELECT '02','05','08'

IF OBJECT_ID('Tb','U') IS NOT NULL 
DROP TABLE Tb

CREATE TABLE Tb(B1 varchar(4),B2 varchar(4),B3 varchar(4))

INSERT INTO Tb(B1,B2,B3)
SELECT '01','02','03' UNION ALL 
SELECT '01','07','09' UNION ALL
SELECT '02','04','05'

SELECT  B1 , B2 , B3 
FROM dbo.Tb B
WHERE NOT EXISTS(SELECT TOP 1 1  FROM Ta WHERE A1 = B.B1 AND A2 = B.B2 AND A3 =  B.B3)

DROP TABLE Ta
DROP TABLE Tb