select * from table2 b 
right outer join table1 a
on a.id=b.id and b.lock='y'
union all
select id,fee1,fee2,fee3 from table1 where lokc='n'
order by id

解决方案 »

  1.   

    也可以这样:
    SELECT * FROM TABLE1 WHERE lock='N'
    UNION ALL
    SELECT * FROM TABLE2 WHERE ID IN (SELECT * FROM TABLE1 WHERE lock='Y')
      

  2.   

    还可以这样:
    SELECT * FROM TABLE1 WHERE lock='N'
    UNION ALL
    SELECT * FROM TABLE2 a,table1 b WHERE a.id=b.id and b.lock='y' 
      

  3.   

    select 
    id=case when a.lock='N' then a.id else b.id end,
    fee1=case when a.lock='N' then a.fee1 else b.fee1 end,
    fee2=case when a.lock='N' then a.fee2 else b.fee2 end,
    fee3=case when a.lock='N' then a.fee3 else b.fee3 end
    from table1 a left join table2 b on a.id=b.id
      

  4.   

    如果你说的条件能保证:
    SELECT * FROM TABLE1 WHERE lock='N'
    UNION ALL
    SELECT * FROM TABLE2 
      

  5.   

    OpenVMS(半知半解) 与我原来的写法一致,速度很慢
      

  6.   

    select id,fee1,fee2,fee3 from table1 where lock ="n"
    union
    select a.id,b.fee1,b.fee2,b.fee3 from table1 a inner jion table2 b on  a.id=b.id where a.lock="Y"
      

  7.   

    SELECT * FROM TABLE1 WHERE lock='N'
    UNION ALL
    SELECT * FROM TABLE2 WHERE ID IN (SELECT ID FROM TABLE1 WHERE lock='Y')
      

  8.   

    Yang_(扬帆破浪) 的写法最简单有效