有三张表:(这三张表中的字段对应记录的关系都是多对多)
业务业务类别关系表  wap_ssrv_servicegroup_map
业务类别栏目关系表  wap_ssrv_catgroup_map
业务栏目关系表      wap_ssrv_servicecatgroup_map表字段如下:
create table wap_ssrv_servicegroup_map(
   servicegroupindex       number(10,0)                                not null,       --业务类别序号
    serviceindex            number(10,0)                                not null,       --业务ID
    serviceid               varchar2(40)                                not null,       --业务代码    constraint pk_wap_ssrv_servicegroup_map primary key (servicegroupindex,serviceindex)
)
/create table wap_ssrv_catgroup_map(
    cat_id     varchar2(32)                     not null, --栏目序号
    servicegroupindex       number(10,0)                     not null,          --业务类别序号
    constraint pk_wap_ssrv_catgroup_map primary key (cat_id,servicegroupindex)
)
/create table wap_ssrv_servicecatgroup_map(
    cat_id     varchar2(32)                     not null, --栏目序号
    serviceindex       number(10,0)                     not null,          --业务ID
    constraint pk_ssrv_servicecatgroup_map primary key (cat_id,serviceindex)
)
/wap_ssrv_servicegroup_map 和 wap_ssrv_catgroup_map表中已有记录,现在想通过SQL的条件入参serviceindex,来查询到该serviceindex对应的业务信息,所对应的栏目有哪些
我写的SQL如下:
select a.cat_id,re_serviceindex from wap_ssrv_catgroup_map a,wap_ssrv_servicegroup_map b where b.servicegroupindex = a.servicegroupindex and b.serviceindex=re_serviceindex;其中re_serviceindex是实参,已经有值传到该SQL里面,查询得到的结果集 则是 serviceindex和catid的对应关系。
但假设wap_ssrv_servicegroup_map 和 wap_ssrv_catgroup_map表的记录分别是下面这样的情形:wap_ssrv_servicegroup_map业务业务类别关系表
1   a
1   b
1   cwap_ssrv_catgroup_map业务类别栏目关系表
a   A
a   B
b   A这样,执行我上面写的那个SQL后,查询出的结果则为:
1   A
1   B
1   A这样就有2条1 A的重复记录,请人各位,针对这样的场景逻辑,有什么更好的SQL来实现么?

解决方案 »

  1.   

    这个结果:
    这样,执行我上面写的那个SQL后,查询出的结果则为: 
    1  A 
    1  B 
    1  A 是不是你想要得到的结果吗?
    还是你不想得到的结果?
    有没有更好的sql语句,那是优化问题
      

  2.   

    还有两个问题:
    1.
    wap_ssrv_servicegroup_map业务业务类别关系表 
    1  a 
    1  b 
    1  c 
    这个表wap_ssrv_servicegroup_map(servicegroupindex,serviceindex ,serviceid)
    有三个字段,而你只给出两列的值,谁知道哪个值对应哪个字段?2.
    select a.cat_id,re_serviceindex from wap_ssrv_catgroup_map a,wap_ssrv_servicegroup_map b where b.servicegroupindex = a.servicegroupindex and b.serviceindex=re_serviceindex; 
    这个查询语句参数re_serviceindex的值是多少?这些隐藏的信息都没有说清楚?什么知道你得到的结果是否正确?
      

  3.   

    不太明白你想做什么...
    如果你不想要两条完全相同的数据,可以用select distinct
    如果你想提高效率,可以考虑用外连接试一下,网上有相关的文章
      

  4.   

    這樣的表結構是會出現這種問題的﹐你要用distinct 來去掉重復