select t1.*,
 (select count(*) from table2 t2 where t2.check=t1.check and rownum=1) as flag
from table1 t1
总觉得这样写方式或效率有问题
能否改成两表关联的形式,或者用where exists...

解决方案 »

  1.   

    select count(*) from table2 t2 where t2.check=t1.check and rownum=1这个不是恒为1吗?这句话有什么用?
      

  2.   

    flag是0或1本想用exists,但是加不了一列flag
      

  3.   

    在t1又在t2的,flag给1,在t1而不在t2的,flag给0,是这个意思不?select t1.*,decode(t2.check,null,0,1) flag
    from table1 t1
    left join table2 on t1.check=t2.check
      

  4.   

    one-to-one或many-to-one可以这样令我郁闷的是many-to-many也就是table2中存在符合条件的flag就置为1
      

  5.   

    我也试过
    不过解释计划没有变化
     SORT AGGREGATE
      COUNT STOPKEY
       TABLE ACCESS BY INDEX ROWID
        INDEX RANGE SCAN(table2.check)
     TABLE ACCESS FULL(table1)
    能否转成:
    nest loop
     TABLE ACCESS FULL(table1)
      INDEX RANGE SCAN(table2.check)
      

  6.   

    select t1.*,decode(t2.check,null,0,1) flag
    from table1 t1
    left join table2 on t1.check=t2.check;
      

  7.   

    非常感谢各位
    再次强调
    many-to-many
    非常郁闷
      

  8.   

    表达错了,不能说是many-to-many^_^table1里面的有一个字段check
    对应table2中字段check值,可能存在多条记录,也可能不存在现在要构造一个结果集:
    select table1.*, flag from ...flag=1表示存在一条或多条,0表示不存在
    也就是
    select t1.*,
     (select count(*) from table2 t2 where t2.check=t1.check and rownum=1) as flag
    from table1 t1
    可是这样嵌套解释计划不理想,最好能类似nest loop的
      

  9.   

    select t1.*,(case when t2.check is null then '0' else then '1' end) as flag
    from table1 t1,t2
    where ti.check=t2.check(+)
    这样不知道行不行,试试