日期:2014-05-16  浏览次数:20795 次

请高手指点多表查询问题
我写的语句如下:
select a.username,b.name from userinfo_portal a left join area_info b on a.id = b.id
where a.username in (select substr(wimaxno,1,8) from applyvoipnumberinfo where status=0);
我先介绍三张表:
userinfo_portal 为用户基本信息表,里面有城市的id,
area_info 为城市信息表里面只在id与城市名
applyvoipnumberinfo 为记录全国各城市申请业务的用户信息,这个表中有点特殊用户名是有加域名的,就像我的的邮箱地址一样,如:test0001@163.com,status为是否处理用户申请的标识,0为未处理,1为已处理。
我现在不知道申请的用户是属于哪个城市,我想结合用户信息表与城市表找出这个申请业务的用户是哪个城市的。
上面这条语句没有报错但查不出数据,单独执行前面和后面的语句是可以查出数据来,请高手指点下是我的语句有问题还是方法不对?

------解决方案--------------------
-- userinfo_portal表的username 字段的数据类型是什么?


-- 注意:如果userinfo_portal表的username 字段的数据类型是char()类型的话,当数据长度不够时,
-- Oracle会自动在其后面补空格,此时你去跟 表 applyvoipnumberinfo 中 wimaxno 字段 的前8个字符去比较,就可能不匹配(userinfo_portal表的username 字段,可能因为后面有空格)
------解决方案--------------------
上面些少了点东西

SQL code

select a.username,b.name 
  from userinfo_portal a,
       area_info b
 where a.id = b.id(+)
   and exists(select 'X'
                from applyvoipnumberinfo c
               where c.status=0
                 and upper(c.wimaxno) like upper(a.username)||'%')