oracle的not in的使用

oracle中not in或者in的使用方法

                            如表 user 

           id                           number                                主健                                     主健ID                                   
           name           varchar              姓名
           pid           varchar         父id

 

 

 

                           user表数据

             id                                      name                                      pid                       
              1                  张三                   1,2,3,5
              2                  李四                   1,2,3,5
              3                  刘德华                   1,2


 

 

 

            查询出所有的id不再pid里面的sql语句写法:

           

错误方法:
sleect * from user u where id not in (u.pid)
会提示无效数字类型,
但是如果是写成这样就可以了:
sleect * from user u where id not in (1,2,3,4)
但是有人会说,把id改成字符串类型应该可以,
很遗憾的说也是不可以的,
因为改成字符串正确的写法是
sleect * from user u where id not in ('1','2','3','4')

所以要解决这个问题可以在in里面嵌套select查询语句就可以解决

sleect * from user u1 where id not in (select u2.pid from u2 where u2.pid=u1 .pid)
这样就可以解决万恶的in语句

posted on 2012-03-09 15:07  阳光总在风雨后001  阅读(24345)  评论(0编辑  收藏  举报

导航