急求一条sql语句?(在线等) thanks!!!!!!!!!!
有两张表employees和roles如下:
employees表以下两个字段:
username roleid
你好 2,3,4
roles表以下字段:
roleid nodeid
1 00110
2 10010
3 11100
4 01100
现在我要根据输入的username值得到nodeid的值?
例如: 输入你好 应该得到 10010和11100和01100三条记录值
我用的是oracle9,能得到上面的结果就可以了,如果能一下得到这三个值的或集最好不过了.
用存储过程和语句都行.
问题点数:20、回复次数:12Top
1 楼wupangzi(无本之木)回复于 2005-01-06 15:12:31 得分 0
怎么显示你的结果呢?
Top
2 楼zzw0598(口是心非)回复于 2005-01-06 15:23:49 得分 0
select nodeid from roles
where roleid in (select roleid from employees where username='你好')Top
3 楼ORARichard(没钱的日子......)回复于 2005-01-06 15:42:15 得分 3
select a.* from roles a,(select roleid||',' roleid from employees where username='你好') b
where instr(b.roleid,a.roleid||',')>0;Top
4 楼CodeMagic(ErrorDetector)回复于 2005-01-06 15:46:26 得分 3
select nodeid from roles where
'('||roleid||',' in (select '('||roleid||')' from employees where username='你好')
or
','||roleid||')' in (select '('||roleid||')' from employees where username='你好')
or
','||roleid||',' in (select '('||roleid||')' from employees where username='你好')Top
5 楼ORARichard(没钱的日子......)回复于 2005-01-06 15:47:59 得分 3
SQL> select * from employees;
USER ROLEI
---- -----
你好 2,3,4
SQL> select * from roles;
ROLEID NODEI
---------- -----
1 00110
2 10010
3 11100
4 01100
SQL> select a.* from roles a,(select roleid||',' roleid from employees where username='你好') b
2 where instr(b.roleid,a.roleid||',')>0;
ROLEID NODEI
---------- -----
2 10010
3 11100
4 01100
SQL>Top
6 楼ORARichard(没钱的日子......)回复于 2005-01-06 15:50:32 得分 3
不对,上面这句只能适应编号为一个数字的。如果能满足楼主的需要就用,不行再改进吧Top
7 楼CodeMagic(ErrorDetector)回复于 2005-01-06 15:54:18 得分 3
楼上的有问题,容易把部分包含的也找出来,例如
username='你好' 的roleid=12,13
roles包含roleid=2,3,12,13的纪录,则把这4条的nodeid都找出来了。
上面还可以简化一些:
select nodeid from roles where
'('||roleid||',' in (select '('||roleid from employees where username='你好')
or
','||roleid||')' in (select roleid||')' from employees where username='你好')
or
','||roleid||',' in (select roleid from employees where username='你好')Top
8 楼ORARichard(没钱的日子......)回复于 2005-01-06 16:11:42 得分 5
select a.* from roles a,(select ','||roleid||',' roleid from employees where username='你好') b
where instr(b.roleid,','||a.roleid||',')>0;
这样行了
Top
9 楼wupangzi(无本之木)回复于 2005-01-06 16:30:31 得分 0
ORARichard(没钱的日子......) ,呀,答对了!哈哈!Top
10 楼joydsj(刘天)回复于 2005-01-06 18:19:30 得分 0
select * from roles where roleid in(select roleid from employee where name='nihao')Top
11 楼zrb007(老兵)回复于 2005-01-07 16:05:35 得分 0
学习,用的很精湛啊:)
Top
12 楼baojianjun(包子)回复于 2005-01-08 09:10:52 得分 0
已經解決了Top




