表A
字段   单号    物品名称
       001      汽车
       001      火车
       001      飞机
       002      汽车
       002      火车
       003      汽车
表B
字段   单号    物品名称
       001      汽车
       001      火车
       002      汽车
要求查询的结果为
       001     飞机
       002     火车
       003     汽车也就是说 如果表A中的单号在表B中出现了 则把在表A中出现没有在表B中出现的物品显示出来,如果单号没有在表B中出现,也显示出来!就这么一个SQL语句 求高手帮忙   

解决方案 »

  1.   

    select * from A left join B on A.单号=B.单号 where A.物品名称 <> B.物品名称 
      

  2.   

    "如果表A中的单号在表B中出现了 则把在表A中出现没有在表B中出现的物品显示出来,如果单号没有在表B中出现,也显示出来"实在不好理解,我怎么感觉你说的就是在A中出现并且在B中没有出现的列出来
      

  3.   

    SELECT A.* FROM A A 
    WHERE NOT EXISTS(
    SELECT 1 FROM B WHERE (A.单号=单号 AND 物品名称<>A.物品名称) 
    OR 
    (A.单号<>单号 AND 物品名称=A.物品名称))
      

  4.   

    create table A (单号 nvarchar(10),  物品名称 nvarchar(10))
    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
      

  5.   

    -- my name is sinpoal
    -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   汽车 */
      

  6.   

    create table #A1
    (
      单号 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 行受影响)
      

  7.   

    create table A (单号 nvarchar(10),  物品名称 nvarchar(10))
    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
      

  8.   


    /*
    -- 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 
      

  9.   

    应用7楼的。我觉得用left join比较好,至少容易理解。适合初学者。
      

  10.   


    select *
    From A 
    where not exists(
        select 1 from B where A.单号=B.单号 and A.物品名称=B.物品名称
    )
     
    的查法应该更好理解点吧,查出在表1中弹不在表2中存在的记录
      

  11.   

    select *
    From A 
    where not exists(
        select 1 from B where A.单号=B.单号 and A.物品名称=B.物品名称
    )
      

  12.   


    select * from A where A.单号 = B.单号 and order  by 单号 
      

  13.   


    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.[物品名称] )
      

  14.   

    select * from A inner join B on A.单号=B.单号 where A.物品名称 <> B.物品名称 
      

  15.   


    select a.* from  a  where  not exists 
    ( select * from b where  [单号] = a.[单号] and [物品名称]=a.[物品名称] )
      

  16.   

    学过集合的差运算吧。
    SQL Server: SELECT 单号,物品名称 FROM A EXCETP SELECT 单号,物品名称 FROM BORACLE不是Except,可以查一下帮助手册。