select t2.姓名,t1.职务名称
where instr(t2.职务ID,t1.职务ID) <> 0
and t1.职务ID = 1select t2.姓名,t1.职务名称
where instr(t2.职务ID,t1.职务ID) <> 0
and t1.职务ID = 2
BTW, I think your table 2 design is something not so good :-)

解决方案 »

  1.   

    select t2.姓名,t1.职务名称
    from t2,t1
    where instr(t2.职务ID,t1.职务ID) <> 0
    and t1.职务ID = 1select t2.姓名,t1.职务名称
    from t2,t1
    where instr(t2.职务ID,t1.职务ID) <> 0
    and t1.职务ID = 2:-)
      

  2.   

    谢谢你black_snail,如果我想查询是队长或者局长的呢
    也就是说,查询结果应该是
    张三、李四都应该有。
      

  3.   

    谢谢你black_snail,如果我想查询是队长或者局长的呢
    也就是说,查询结果应该是
    张三、李四都应该有。应该为张三    队长 局长
    李四    队长
      

  4.   

    按black_snail兄这种做法,比如还有职务为“小”‘队长’,那么统计会出错的。
    但是我还没想出解决办法,另外严重同意关于表2的设计问题的看法。
      

  5.   

    看错了,应该举的例子为:职务ID为11和ID为1的
      

  6.   

    create or replace type mytabletype as table of number;
    /create or replace function strtab(p_str in varchar2)
    return mytabletype
    as
    lstr varchar2(1000) default p_str||',';
    ln   number;
    ldata   mytabletype:=mytabletype();
    begin
    loop
      ln:=instr(lstr,',');
      exit when (nvl(ln,0)=0);
      ldata.extend;
      ldata(ldata.count):=ltrim(rtrim(substr(lstr,1,ln-1)));
      lstr:=substr(lstr,ln+1);
    end loop;
    return ldata;
    end;
    /SQL> select * from table(cast(strtab('11,12,13') as mytabletype));COLUMN_VALUE
    ------------
              11
              12
              13SQL> create table bb(id varchar2(2),name varchar2(10));Table createdSQL> insert into bb values('11','张三');1 row insertedSQL> insert into bb values('12','李四');1 row insertedSQL> insert into bb values('13','王五');1 row insertedSQL> select * from bb where id in (select * from table(cast(strtab('11,12,13') as mytabletype)));ID NAME
    -- ----------
    11 张三
    12 李四
    13 王五参考上面做法 ,设计由行转为列
      

  7.   

    在字串两端补个逗号吧。
    这样就不会找到小队长了。:)select t2.姓名,t1.职务名称
    from t2,t1
    where instr(','||t2.职务ID||','  ,  ','||t1.职务ID||',') <> 0
    and t1.职务ID = 1同时找就不叫麻烦了。select t2.姓名,max(t1.职务名称)|| min(t1.职务名称) as 职务名称
    from t2,t1
    where instr(','||t2.职务ID||','  ,  ','||t1.职务ID||',') <> 0
    and (t1.职务ID = 1 or t1.职务ID = 2)
    group by t2.姓名仅限于两个职务。说实话你这样的表是设计的时候轻松,写程序累死。
    还是改掉吧。以后如果还要分类统计的话,
    你会想自杀的。
      

  8.   

    black_snail 我要给你30分
    beckhambobo 我要给你20分
    还有50分,快来得啊
      

  9.   

    你的是对的: iwantsay(吵闹) ( ) 信
      

  10.   

    black_snail的方法是OK的。
    但是建议你把表一的结构改为两个主键:(姓名 PK,性别, 职务ID PK)
    对于这种需求,多个主键很合适!这样表一的数据就变为:
    姓名         性别      职务ID
    张三         男         1
    张三         男         2
    李四         男         1
    王五         男         3查询时:
    找队长
    select t1.姓名,t2.职务名称 from t1,t2 
    where t1.职务ID=t2.职务ID and t1.职务ID = 1
    找局长
    select t1.姓名,t2.职务名称 from t1,t2 
    where t1.职务ID=t2.职务ID and t1.职务ID = 2