有张表A
id    name
1     a
2     b
3     c我想用id查出name 
select name from A where id in(1,2,3,4,5)
用id in 去查 例如上面那句 但是结果不是3条 要5条记录 4,5没有 那就空着

解决方案 »

  1.   

    select b.id,a.name from a right join
    (select rownum id from dual connect by rownum<10)b
    on b.id=a.id
    where b.id in(1,2,3,4,5)
      

  2.   

    能不能不用oracle特有的东西 用标准sql写
      

  3.   

    那就没什么好办法
    你的要求不太合理,要将原表中没有的数据凭空生出几条来,这些条数还不确定
    如果id in(1,2,3,4,5) 括号里的值为5个的话,可以试试
    select * from(
    select id,name from a union all
    select null,null from a union all
    select null,null from a union all
    select null,null from a union all
    select null,null from a union all
    select null,null from  order by id)
    where (id in(1,2,3,4,5) or id is null)
    and rownum<=5rownum貌似在mssql里也没有,mssql里用top5吧
      

  4.   

    order by id 改成order by 1
    或者将1楼里(select rownum id from dual connect by rownum <10)
    替换成select 1 from dual union all 
      select 2 from ...
    一条一条写出来
      

  5.   

    找到解决办法了 那些id是从A何另外几张表里查出来的 用union可以解决我的问题 呵呵
      

  6.   

    单纯从技术上,可以这样来做,
    SQL>  with temp as
      2   (
      3   select 1 id from dual
      4   union
      5   select 2 id from dual
      6   union
      7   select 3 id from dual
      8   )
      9  select temp.*, b.t from temp, (select substr(r, decode(rownum, 1, 0, instr(
    r,',',1,rownum-1))+1, instr(r,',',1,rownum)-decode(rownum, 1, 0, instr(r,',',1,r
    ownum-1))-1) t from (select '1,2,3,4,5'||',' r from dual) connect by instr(r,','
    ,1,rownum)>0) b where temp.id(+)=b.t;        ID T
    ---------- --------------------
             1 1
             2 2
             3 3
               5
               4