CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Oracle >  开发

急求一条sql语句?(在线等) thanks!!!!!!!!!!

楼主yyhe(software)2005-01-06 15:06:58 在 Oracle / 开发 提问

有两张表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

相关问题

  • sql语句,急
  • sql语句 急急
  • 急急,sql语句!
  • sql语句急救
  • 急用!!sql语句
  • 急求SQL语句!!!!!!!!!!!!!
  • 求SQL语句急
  • 急问!!!!!!!!!!!!!!!!!!!!!SQL语句
  • 急求SQL语句?
  • 急求SQL语句

关键词

  • roleid
  • 你好
  • nodeid
  • employee where
  • roles
  • username
  • employee
  • 得到
  • where
  • select

得分解答快速导航

  • 帖主:yyhe
  • ORARichard
  • CodeMagic
  • ORARichard
  • ORARichard
  • CodeMagic
  • ORARichard

相关链接

  • Oracle类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo