表A
字段 单号 物品名称
001 汽车
001 火车
001 飞机
002 汽车
002 火车
003 汽车
表B
字段 单号 物品名称
001 汽车
001 火车
002 汽车
要求查询的结果为
001 飞机
002 火车
003 汽车也就是说 如果表A中的单号在表B中出现了 则把在表A中出现没有在表B中出现的物品显示出来,如果单号没有在表B中出现,也显示出来!就这么一个SQL语句 求高手帮忙
字段 单号 物品名称
001 汽车
001 火车
001 飞机
002 汽车
002 火车
003 汽车
表B
字段 单号 物品名称
001 汽车
001 火车
002 汽车
要求查询的结果为
001 飞机
002 火车
003 汽车也就是说 如果表A中的单号在表B中出现了 则把在表A中出现没有在表B中出现的物品显示出来,如果单号没有在表B中出现,也显示出来!就这么一个SQL语句 求高手帮忙
WHERE NOT EXISTS(
SELECT 1 FROM B WHERE (A.单号=单号 AND 物品名称<>A.物品名称)
OR
(A.单号<>单号 AND 物品名称=A.物品名称))
create table B (单号 nvarchar(10), 物品名称 nvarchar(10))insert into A select
'001','汽车' union all select
'001','火车' union all select
'001','飞机' union all select
'002','汽车' union all select
'002','火车' union all select
'003','汽车' insert into B select
'001','汽车' union all select
'001','火车' union all select
'002','汽车'
select A.*
From A
left Join B On A.单号=B.单号 and A.物品名称=B.物品名称
where B.单号 is null
/*单号 物品名称
---------- ----------
001 飞机
002 火车
003 汽车(3 行受影响)*/
drop table a,b
-create table a(hao char(3),name char(4))
insert a
select '001','汽车' union all
select '001','火车' union all
select '001','飞机' union all
select '002','汽车' union all
select '002','火车' union all
select '003','汽车'
create table b(hao char(3),name char(4))
insert b
select '001','汽车' union all
select '001','火车' union all
select '002','汽车' ------------查询-----------
select ([hao]+[name]) as 结果 into hi from b
select ([hao]+[name]) as 结果 into hh from a
select distinct hh.* from hh,hi
where hh.结果 not in (select 结果 from hi)/* 结果
--------
001 飞机
002 火车
003 汽车 */
(
单号 varchar(20),
物品名称 varchar(20)
)
insert into #A1 select '001','汽车'
insert into #A1 select '001','火车'
insert into #A1 select '001','飞机'
insert into #A1 select '002','汽车'
insert into #A1 select '002','火车'
insert into #A1 select '003','汽车'
create table #B2
(
单号 varchar(20),
物品名称 varchar(20)
)
insert into #B2 select '001','汽车'
insert into #B2 select '001','火车'
insert into #B2 select '002','汽车'
select A.* from #A1 A where not exists(select * from #B2 B where B.单号=A.单号 and B.物品名称=A.物品名称)单号 物品名称
-------------------- --------------------
001 飞机
002 火车
003 汽车(3 行受影响)
create table B (单号 nvarchar(10), 物品名称 nvarchar(10))insert into A select
'001','汽车' union all select
'001','火车' union all select
'001','飞机' union all select
'002','汽车' union all select
'002','火车' union all select
'003','汽车' insert into B select
'001','汽车' union all select
'001','火车' union all select
'002','汽车'
select *
From A
where not exists(
select 1 from B where A.单号=B.单号 and A.物品名称=B.物品名称
)
/*单号 物品名称
---------- ----------
001 飞机
002 火车
003 汽车(3 行受影响)*/
drop table a,b
/*
-- Author:SQL77--RICHIE
-- Version:V1.001 Date:2008-05-15--转Flystone*/-- Test Data: TB
If object_id('TB') is not null
Drop table TB
Go
Create table TB(单号 VARCHAR(10),物品名 VARCHAR(10))
Go
Insert into TB
SELECT '001', '汽车'UNION ALL
SELECT '001', '火车'UNION ALL
SELECT '001', '飞机'UNION ALL
SELECT '002', '汽车'UNION ALL
SELECT '002', '火车' UNION ALL
SELECT '003', '汽车' Go-- Test Data: TB1
If object_id('TB1') is not null
Drop table TB1
Go
Create table TB1(单号 VARCHAR(10),物品名 VARCHAR(10))
Go
Insert into TB1
SELECT '001', '汽车'UNION ALL
SELECT '001', '火车'UNION ALL
SELECT '002', '汽车'--Start
SELECT A.* FROM TB A
WHERE NOT EXISTS(SELECT * FROM TB1 WHERE (A.单号=单号 AND 物品名=A.物品名) )单号 物品名
---------- ----------
001 飞机
002 火车
003 汽车(所影响的行数为 3 行)
--Result:
/*
*/
--End
select *
From A
where not exists(
select 1 from B where A.单号=B.单号 and A.物品名称=B.物品名称
)
的查法应该更好理解点吧,查出在表1中弹不在表2中存在的记录
From A
where not exists(
select 1 from B where A.单号=B.单号 and A.物品名称=B.物品名称
)
select * from A where A.单号 = B.单号 and order by 单号
declare @t1 table( [单号] varchar(20) , [物品名称] varchar(20))
insert into @t1 values('001' , '汽车')
insert into @t1 values('001' , '火车')
insert into @t1 values('001' , '飞机')
insert into @t1 values('002' , '汽车')
insert into @t1 values('002' , '火车')
insert into @t1 values('003' , '汽车') declare @t2 table( [单号] varchar(20) , [物品名称] varchar(20))
insert into @t2 values('001' , '汽车')
insert into @t2 values('001' , '火车')
insert into @t2 values('002' , '汽车') select a.* from @t1 a where not exists
( select * from @t2 where [单号] = a.[单号] and [物品名称]=a.[物品名称] )
select a.* from a where not exists
( select * from b where [单号] = a.[单号] and [物品名称]=a.[物品名称] )
SQL Server: SELECT 单号,物品名称 FROM A EXCETP SELECT 单号,物品名称 FROM BORACLE不是Except,可以查一下帮助手册。