两个表table1,table2,table1.name=table2.name,table1.code=table2.code,现在想跟据这个条件查询出table2中table1.id<>table2.id的数据,这样写可以吗?select table1.id, table1.code, table2.id, table2.code
  from table1 a
  left join table2 b
    on table1.code = table2.code
   and table1.name = table2.name
   and table1.id <> table2.id
 order by b.code

解决方案 »

  1.   

    select * from table2 t2
    minus
    select t3.* from table2 t3, table1 t1
    where t1.name = t3.name and t1.code = t3.code and t1.id = t3.id;select * from table2 t2
    where not exists 
    ( select 1 from table1 t1 where t1.name = t2.name and t1.code = t2.code and t1.id = t2.id);
      

  2.   

    你这样写也没有错误,最好用别名了,符合语法规则了。
    select table1.id, table1.code, table2.id, table2.code 
      from table1 a 
      left join table2 b 
        on a.code = b.code 
      and a.name = b.name 
      and a.id <> b.id 
    order by b.code
      

  3.   

    肯定是不行的 
        on table1.code = table2.code 
        and table1.id <> table2.id 
    这俩矛盾, 为什么不这么写呢,select table1.id, table1.code, table2.id, table2.code 
      from table1 a,
      table2 b 
        where table1.code(+) = table2.code 
      and table1.name = table2.name 
    order by b.code取table2的全部呢
      

  4.   

    select * from table2 t2
    where not exists 
    ( select 1 from table1 t1 where t1.name = t2.name and t1.code = t2.code and t1.id = t2.id);支持这个
      

  5.   

     在select 里加个子查询不就ok了
      

  6.   

    (select t2.*,table.id from table2 t2 ,table1 where t2.name=table1.name and t2.code=table1.code)
    minus
    select t3.*,t1.id from table2 t3, table1 t1
    where t1.name = t3.name and t1.code = t3.code and t1.id = t3.id;
    ---这个应该可以吧
      

  7.   

    如果只想取出id不同的数据,那么就不需要外连接了
    select a.id, a.code, b.id, b.code 
      from table1 a 
      inner join table2 b 
        on a.code = b.code 
      and a.name = b.name 
      and a.id <> b.id 
    order by b.code
      

  8.   

    1、如果是想这么去数据,像狂狼兄所言,外连接是没有效果的,原因不用我说。
    2、如果(table1.code,table1.name)和(table2.code,table2.name)可以分别唯一的确定
       table1中和table2中的一条数据,你这么写是没有问题的,如果不是就不可以了,会产生笛卡尔积
    3、最保险的方法还是用 1 楼的第二种方法。
      

  9.   

    用not exists,然后 在select后面加一个子查询吧