在开发中遇到一个问题,类似代码我编了一个,如下:select tb1.*,td.flagg from (
    select 1 t1 from dual
    UNION
    select 2 t1 from dual
     UNION
    select 3 t1 from dual)  tb1
    
    left join 
        (select 'Y' flagg from dual) td
        on exists
        ( select t1 from 
           ( select 1 t1 from dual
             UNION
             select 2 t1 from dual)  tb2
             where tb1.t1 = tb2.t1)
运行结果是:
T1      FLAGG
1 Y
2 Y
3 Y期望结果是:
T1      FLAGG
1 Y
2 Y
3 null有高手能解释一下吗?

解决方案 »

  1.   


    select tb1.*,(case when tb1.t1=tb2.t1 then 'y' else null end) from (
      select 1 t1 from dual
      UNION
      select 2 t1 from dual
      UNION
      select 3 t1 from dual) tb1
    left join
       ( select 1 t1 from dual
      UNION
      select 2 t1 from dual) tb2
       on tb1.t1 = tb2.t1       T1 (
    --------- -
            1 y
            2 y
            3
      

  2.   

    哥哥 你select tb1.*,td.flagg  td.flagg 不全都是'y’么 如何达到效果
      

  3.   

    你这根本跟是否是外连接无关!因为你固定写死了要查询出td.flagg 
      

  4.   

    不是有on exist 条件吗?难道不起作用?
      

  5.   

    这样是对的,但我不是要一个结果,因为考虑实际应用的SQL 的复杂性和性能,用on exists性能高
      

  6.   


    你觉得你 exist达到效果没  完全没有啊  3都被显示出来了
      

  7.   

    表td和tb1没关联啊 left join没效果
      

  8.   

    可以写成这样,:)
    select tb1.*,case when exists
       ( select t1 from 
         ( select 1 t1 from dual
           UNION
          select 2 t1 from dual)  tb2
        
        where tb1.t1 = tb2.t1) then 'Y' else 'N' end tttt from (
        select 1 t1 from dual
        UNION
        select 2 t1 from dual
         UNION
        select 3 t1 from dual)  tb1谢谢各位参与