A表如下
--------------------------------------
学号       座位号           学生ID
1            02             000012
2            03             000013
3            04             000014
4            05             000015
.......B表也有相同字段
-------------------------------------
学号       座位号           学生ID
1            02             000012
null        null            000013
null        null            000014
4            05             000015
其中 A B表的主键都是学生ID,我现在使用SQL查询
select a.学号,b.学号,a.座位号,b.座位号 from A a,B b
where a.学生ID=b.学生ID
and (a.学号<>b.学号  or a.座位号<>b.座位号)
查出来无记录按理说B表里面有两条为空。。应该跟A表的那两个对不上啊。。为什么没查出来呢。
我就是要查出AB表ID相等,学号或者座位号两个表对不上的记录啊。

解决方案 »

  1.   

    (a.学号<>b.学号 and a.学号 is not null and b.学号 is not null) or (座位号一样的)
     -- null不用理的话
      

  2.   

    null  和数字 可以用<>表示么?
      

  3.   

    因为null
    use tempdb;
    /*
    create table A
    (
    学号 int not null,
    座位号 nvarchar(10) not null,
    学生ID nvarchar(10) not null
    );
    insert into A(学号,座位号,学生ID)
    values
    (1,'02','000012'),
    (2,'03','000013'),
    (3,'04','000014'),
    (4,'05','000015');create table B
    (
    学号 int,
    座位号 nvarchar(10),
    学生ID nvarchar(10) not null
    );
    insert into B(学号,座位号,学生ID)
    values
    (1,'02','000012'),
    (null,null,'000013'),
    (null,null,'000014'),
    (4,'05','000015');
    */
    select a.学号,b.学号,a.座位号,b.座位号 
    from A as a,B as b
    where a.学生ID = b.学生ID
    and (a.学号<>isnull(b.学号,0) or a.座位号<>isnull(b.座位号,0));
      

  4.   

    请问:null能用<>来比较吗?
    用is null 和is not null来比较的!
      

  5.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2011-03-25 10:10:01
    -- Verstion:
    --      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    -- Jul  9 2008 14:43:34 
    -- Copyright (c) 1988-2008 Microsoft Corporation
    -- Enterprise Edition on Windows NT 5.2 <X86> (Build 3790: Service Pack 2)
    --
    ----------------------------------------------------------------
    --> 测试数据:[A]
    if object_id('[A]') is not null drop table [A]
    go 
    create table [A]([学号] int,[座位号] varchar(2),[学生ID] varchar(6))
    insert [A]
    select 1,'02','000012' union all
    select 2,'03','000013' union all
    select 3,'04','000014' union all
    select 4,'05','000015'
    --> 测试数据:[B]
    if object_id('[B]') is not null drop table [B]
    go 
    create table [B]([学号] int,[座位号] varchar(2),[学生ID] varchar(6))
    insert [B]
    select 1,'02','000012' union all
    select null,null,'000013' union all
    select null,null,'000014' union all
    select 4,'05','000015'
    --------------开始查询--------------------------
    select
     a.学号,b.学号,a.座位号,b.座位号 
     from
      a ,b
      where CHECKSUM(a.学号,a.座位号) not in (select CHECKSUM(学号,座位号) from b)
      and
      a.学生ID=b.学生ID
    ----------------结果----------------------------
    /* 学号          学号          座位号  座位号
    ----------- ----------- ---- ----
    2           NULL        03   NULL
    3           NULL        04   NULL(2 行受影响)
    */
      

  6.   

    and (a.学号<>b.学号 or a.座位号<>b.座位号)and (isnull(a.学号,'')<>isnull(b.学号,'') or isnull(a.座位号,'')<>isnull(b.座位号,''))
      

  7.   


    create table [B]([学号] int,[座位号] varchar(2),[学生ID] varchar(6))
    insert [B]
    select 1,'02','000012' union all
    select null,null,'000013' union all
    select null,null,'000014' union all
    select 4,'05','000015'
    gocreate table [A]([学号] int,[座位号] varchar(2),[学生ID] varchar(6))
    insert [A]
    select 1,'02','000012' union all
    select 2,'03','000013' union all
    select 3,'04','000014' union all
    select 4,'05','000015'
    goselect a.学号,b.学号,a.座位号,b.座位号 
    from a,b
    where a.学生ID=b.学生ID and (isnull(a.学号,'')<>isnull(b.学号,'') or isnull(a.座位号,'')<>isnull(b.座位号,''))drop table a,b
    /*
    学号          学号          座位号  座位号
    ----------- ----------- ---- ----
    2           NULL        03   NULL
    3           NULL        04   NULL(2 行受影响)