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 :-)
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 :-)
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的设计问题的看法。
/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 王五参考上面做法 ,设计由行转为列
这样就不会找到小队长了。:)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.姓名仅限于两个职务。说实话你这样的表是设计的时候轻松,写程序累死。
还是改掉吧。以后如果还要分类统计的话,
你会想自杀的。
beckhambobo 我要给你20分
还有50分,快来得啊
但是建议你把表一的结构改为两个主键:(姓名 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