有三张表:(这三张表中的字段对应记录的关系都是多对多)
业务业务类别关系表 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来实现么?
业务业务类别关系表 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来实现么?
解决方案 »
- 安装Oracle失败,提示需要注册wfnvg.ocx控件!如何解决
- pl/sql中的select into问题
- 有遇到过么:如何得到矩形区内实体的坐标?实体存在ORACLE SPATIAL中
- 调用存储过程中,输入参数出错:非法的变量名/编号,源代码见内。
- foxpro 倒出的dbf文件,如何倒入到oracle中
- 请教在Oracle中怎样实现 像在SQL Server中实现 作业 的功能
- 如何查出这样的“断号”?
- 关于主键与外键的索引问题
- oracle里在一个表里任意取10条数据,怎么写?
- ORACLE服务器装了两个网卡,怎么把ORACLE绑定到另一个网卡上
- 统计 sql 语句
- 使用BIRT报表,出现的ORA-00020(超过最大连接数)的问题
这样,执行我上面写的那个SQL后,查询出的结果则为:
1 A
1 B
1 A 是不是你想要得到的结果吗?
还是你不想得到的结果?
有没有更好的sql语句,那是优化问题
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的值是多少?这些隐藏的信息都没有说清楚?什么知道你得到的结果是否正确?
如果你不想要两条完全相同的数据,可以用select distinct
如果你想提高效率,可以考虑用外连接试一下,网上有相关的文章