刚接触数据库,有点晕
select * from table1 b ,score a where a.score <> b.score 这个语句,表table1是score复制过去的,那这样的语句运行的结果不应该是空集嘛?那为什么我实际运行的结果不是呢?

解决方案 »

  1.   

    select * from table1 b ,score a where a.score <> b.score这个是先产生两表数据的笛卡尔积,然后再筛选出两个score不等的数据集!
      

  2.   

    怎么会是空集呢。除非你的score都一样
      

  3.   

    例如:A  
    id------score
    1---------1
    2---------2按楼主的语句先产生的笛卡尔积如下a.score-------b.score
    1-----------------1
    1-----------------2  --不同
    2-----------------1
    2-----------------2  --不同找出的记录会有两条
      

  4.   

    这个相当于
    select * from table1 b cross join score a where a.score <> b.score
      

  5.   

    score 是一样的 因为table1是score复制过去的啊
    那如果要实现对score这一列的比较,正确的语句应该怎么写呢?
      

  6.   


    select * from table1 b ,score a where a.关联字段 = b.关联字段 and a.score <> b.score--不过这样意义不大,不管A表数据如何,这个查询始终的空!
      

  7.   

    AcHerat
    我实际运行的结果 跟你标示出的很像
    那如果我要实现两列之间的依次比较 而不是先笛卡尔 语句应该是怎样的呢?
      

  8.   

    两列的话可以直接比较如 A表有  id  m  n   三个字段,比较的是 n 和 mselect * from tb where n <> m
      

  9.   

    晕 我没说清楚 应该是怎样实现两个表中的两列的比较,比如两个表是table,score,要比较它们中的score列,要求依次比较而不是像我写的那样笛卡尔先,应该是怎么样的呢?
      

  10.   


    --除非Score表的字段Score值对于所有的记录都一样,不然以下就肯定会有结果集
    select * from table1 b ,score a where a.score <> b.score--举例create table #A(Score int, re varchar(10))
    --Score不一样的情况
    insert into #A
    select 1,'2' union all
    select 2,'3'select * into #B from #A
    --结果
    select * from #A as a,#B as b where a.Score<>b.score
    Score       re     Score       re
    ----------- ---------- ----------- ----------
    2           3          1           2
    1           2          2           3(2 行受影响)
    ----------------Score一样的情况
    delete from #Ainsert into #A
    select 1,'2' union all
    select 1,'3'select * into #C from #A--结果
    select * from #A as a,#C as b where a.Score<>b.scoreScore       re     Score       re
    ----------- ---------- ----------- ----------(0 行受影响)
      

  11.   


    select * from table1 b
    left join score a 
    on a.tid=b.tid
    where a.score <> b.score 
      

  12.   


    select * from table1
    except
    select * from table2
    如果用2表连接的方法,内部总会先建立笛卡尔积的
    还有么用游标
      

  13.   

    建议楼主多看看数据库基础
    数据库连接是笛卡尔集,排除score 相等的罢了