我有两张表
A表中有 nos,gxrq两个字段,
B表中有 nos,gxrq两个字段,
需求是这样的:
我要根据传入的单号,比如是Test001, 把A表中单号是Test001, 该单号在B表中不存在/存在但gxrq不同的查出来
上面是两种情况,用SQL该如何写呢?

解决方案 »

  1.   

    select *  from A where nos='TEST001' and not exists (select 1  from  B where nos ='Test001')
    select *  from A where nos='TEST001' and  exists (select 1  from  B where nos ='Test001' and A.gxrq<>B.gxrq)
      

  2.   

    try
    select a.* from a 
    where nos='Test001'
    and
    (not exists(select 1 from b where nos=a.nos)
    or
    exists(select 1 from b where nos=b.nos and gxrq!=a.gxrq)
    )
      

  3.   

    沒明白哈~!只查具體的某張單?
    select *
    from a
    where exists (select 1 from b where nos = a.nos and gxrq <> a.gxrq)
       or not exists (select 1 from tb where nos = a.nos)
    ?
      

  4.   

    建议给出测试数据及你想要的结果。select * from A表 a where nos='Test001'
    and 
    ( not exists (select top 1 * from B表 b where b.nos=a.nos)
    or
    exists (select top 1 * from B表 b where b.nos=a.nos and a.gxrq<>b.gxrq)
    )
      

  5.   

    select *  from A where nos='TEST001' and not exists (select 1  from  B where nos ='Test001' and A.gxrq=B.gxrq这种两种情况都包含吗?
      

  6.   

    具体需求是这样的,B表放的数据是A表中传输过的,如果A表中的单据号在B表中不存在,那么我就传输到目标库中,如果单号存在并且gxrq相同,那么我就不再传输了,但是如果单号存在并且gxrq不同,那么我还是要传输这张单据到目标库中的
      

  7.   


    --得到一個具體的單號 @nos 和 gxrq 值 @gxrq 傳輸的。
    if exists (select 1 from b where nos = @nos and gxrq <> @gxrq)
       or not exists (select 1 from tb where nos = @nos)
    --傳輸處理--或者--傳輸處理的SQL
    insert into ...
    select ...
    from a,...
    where a.nos = '' and ....
      and exists (select 1 from b where nos = a.nos and gxrq <> a.gxrq) --加where條件後邊
       or not exists (select 1 from tb where nos = a.nos)
      

  8.   

    这太绕了,select * from a where nos='test001' and (not exists(select nos from b where nos=a.nos)
    or
    exists(select nos from b where nos=b.nos and gxrq!=a.gxrq)
    )
      

  9.   

    有点绕啊select * from a where nos='test001' and (not exists(select nos from b where nos=a.nos)
    or
    exists(select nos from b where nos=b.nos and gxrq!=a.gxrq)
    )
      

  10.   

    select *
    from a
    where exists (select 1 from b where nos = a.nos and gxrq <> a.gxrq)
       or not exists (select 1 from tb where nos = a.nos)
      

  11.   

    CREATE TABLE TA(NOS VARCHAR(20),GXRQ INT)
    CREATE TABLE TB(NOS VARCHAR(20),GXRQ INT)INSERT INTO TA VALUES('Test001',1),('Test002',1),('Test003',1)
    INSERT INTO TB VALUES('Test004',1),('Test002',1),('Test003',2)SELECT * FROM TA A
    WHERE EXISTS(SELECT * FROM TB B WHERE A.NOS=B.NOS AND A.GXRQ<>B.GXRQ )
    OR NOT EXISTS(SELECT * FROM tb B WHERE A.NOS=B.NOS AND A.GXRQ=B.GXRQ)