刚才一位同学提到的:insert into t111 select '张一','000000'
insert into t111 select '张二','1111'
insert into t111 select '张三','222222222'
insert into t222 select '李1','1111'
insert into t222 select '张一','000000'
insert into t222 select '张二','1111'
insert into t222 select '张四','1111'Select b.[name],b.tel From t111 a , t222 b Where (b.name not in (Select [name] From t111))
Select distinct b.[name],b.tel From t111 a , t222 b Where (b.name not in (Select [name] From t111))为何第一个查询返回6条结果:
李1 1111
李1 1111
李1 1111
张四 1111
张四 1111
张四 1111
第二个查询结果才是想要的:
李1 1111
张四 1111求解

解决方案 »

  1.   

    distinct 是去掉重复行的,你t1表和t2表中的数据大多相同 不去重复行 是得不到你想要的结果的
      

  2.   

    distinct  去掉重复的name
      

  3.   

    其实写成这样就不会了:select *
    from t222
    where name not in ( select name from t111 )
      

  4.   

    原因出在这句话上:“From t111 a , t222 b”
    这句话会对t111和t222做笛卡尔积,产生一个大表,在这个表中,原t222的每条记录都会重复出现3次(为什么是3次?因为t111的总记录数为3),select的时候是select这个大表,所以会出现重复3次的记录,需要用distinct对重复值进行消除。
      

  5.   

    lz:distinct这个关键字是去掉从重复行,如果你有字段的话,他会对第一个字段去重。
      

  6.   

    From t111 a , t222 b
    这个是笛卡尔积