select a.* from a and b where a.col1=b.col1 and a.col2=b.col2 and a.col3=b.col3a表中有二条数据,b表中有十条数据。
上面的查询语句查出的是十条。我的意思应该是从a表中查出数据。a表中一共才有二条数据,查出的应该是0条,1条,或2条。不应该出十条。
请问怎么改?多谢

解决方案 »

  1.   

    如果存在a.col1=b.col1 and a.col2=b.col2 and a.col3=b.col3 就是十条.最好给出表结构,测试数据,计算方法和正确结果.
      

  2.   

    select a.* from a,b where a.col1=b.col1 and a.col2=b.col2 and a.col3=b.col3 
    楼主的语法有问题,是拷贝错了?
    应该是b中满足连接条件的,对应a表的1条记录,b表有多条记录满足条件。所以返回10条
    楼主可改成
    select * from a where not exists(
      select 1 from b where a.col1=b.col1 and a.col2=b.col2 and a.col3=b.col3 )
      

  3.   

    select * from a where exists(
      select 1 from b where a.col1=b.col1 and a.col2=b.col2 and a.col3=b.col3 )