语句1:检索记录是5条(正确结果)。
select ri.Remind_Info_Id,......
from Remind_Info ri, ......
where ......
and (
to_number((select count(*) from Remind_Target rt where ri.remind_Info_Id = rt.remind_Info_Id))
>
to_number((select count(*) from Read_Remind readR where ri.remind_Info_Id = readR.remind_Entity_Id and readR.remind_Resource_Type_Id = 1 and readR.close_Flag = 1 ))
)
语句2:检索记录是2条(错误结果)。
select ......
from Remind_Info ri, ......
where ......
and (
(select count(*) from Remind_Target rt where ri.remind_Info_Id = rt.remind_Info_Id)
>
(select count(*) from Read_Remind readR where ri.remind_Info_Id = readR.remind_Entity_Id and readR.remind_Resource_Type_Id = 1 and readR.close_Flag = 1 ))
)
数据库Remind_Info表中有5条记录,而其中Read_Remind中的数据有2条记录对应于Remind_Info检索出了的2条记录,就是错误结果的2条记录。说明有3条记录是因为Read_Remind表没有对应于Remind_Info表的相关记录造成的。
问:为什么会有这样的结果,出了我提出的语句1的解决方案,还有比较合理的解决办法吗?
select ri.Remind_Info_Id,......
from Remind_Info ri, ......
where ......
and (
to_number((select count(*) from Remind_Target rt where ri.remind_Info_Id = rt.remind_Info_Id))
>
to_number((select count(*) from Read_Remind readR where ri.remind_Info_Id = readR.remind_Entity_Id and readR.remind_Resource_Type_Id = 1 and readR.close_Flag = 1 ))
)
语句2:检索记录是2条(错误结果)。
select ......
from Remind_Info ri, ......
where ......
and (
(select count(*) from Remind_Target rt where ri.remind_Info_Id = rt.remind_Info_Id)
>
(select count(*) from Read_Remind readR where ri.remind_Info_Id = readR.remind_Entity_Id and readR.remind_Resource_Type_Id = 1 and readR.close_Flag = 1 ))
)
数据库Remind_Info表中有5条记录,而其中Read_Remind中的数据有2条记录对应于Remind_Info检索出了的2条记录,就是错误结果的2条记录。说明有3条记录是因为Read_Remind表没有对应于Remind_Info表的相关记录造成的。
问:为什么会有这样的结果,出了我提出的语句1的解决方案,还有比较合理的解决办法吗?
解决方案 »
- 虚拟机如果访问主机oracle数据库
- 大概是安装方面的一个问题吧~小问一下
- sqlldr 可以用来给db link的table 装载数据吗?
- ORA-00933: SQL 命令未正确结束
- 请问driving_site中包含多个表的语法?????select /*+driving_site(table1,table2,table3)*/??????
- tns:连接超时
- oracle连接错误
- oracle 9i转成oracle 10g中long类型问题
- jsp连接oracle,报这个错误:the network adapter could no establish the connection
- 请问高手,如何知道一个表里的一个字段给那个表的外键,我需要把表名,名段名查出来。
- oracle不同版本或平台SQL语句语法不一样吗?
- 请教高手!
select ......
from Remind_Info ri, ......
where ......
and (
(select count(*) from Remind_Target rt where ri.remind_Info_Id = rt.remind_Info_Id(+))
>
(select count(*) from Read_Remind readR where ri.remind_Info_Id = readR.remind_Entity_Id(+) and readR.remind_Resource_Type_Id = 1 and readR.close_Flag = 1 ))
)
检索结果还是2条,为什么右连接也不行?
一条Remind_Info提醒记录对应多条Remind_Target提醒人。
Read_Remind表是提醒人读过的提醒信息。
每一个Remind_Target提醒人要是读过Remind_Info提醒记录,要生成对应的一条Read_Remind的记录。现在的表数据是:Remind_Info5条提醒记录,Remind_Target9条记录,Read_Remind2条记录。
Remind_Info表数据和Remind_Target对应
记录1:有2个提醒人
记录2:有1个提醒人
记录3:有2个提醒人
记录4:有2个提醒人
记录5:有2个提醒人Read_Remind表数据
记录1:对应Remind_Info记录1有1个提醒人读过记录
记录2:对应Remind_Info记录2有1个提醒人读过记录检索结果是Remind_Info表的记录1、记录2检索出来了,而记录3、记录4、记录5我的理解是因为Read_Remind表没有对应数据所以没有检索出来,只能检索到2条记录。
Read_Remind表数据
记录1:对应Remind_Info记录1有1个提醒人读过记录
记录2:对应Remind_Info记录2有1个提醒人以后提醒记录检索结果是Remind_Info表的记录1检索出来了,而记录2没有检索出了,但是Read_Remind表有记录,readR.close_Flag=0?而记录3、记录4、记录5我的理解是因为Read_Remind表没有对应数据所以没有检索出来,只能检索到2条记录。数据库:Remind_Info
REMIND_INFO_ID CONTENT REMIND_TYPE_ID END_TIME CLOSE_FLAG
1 1315 提醒内容1 1 2007-12-13 11:07 0
2 1318 提醒内容2 1 2007-12-13 11:07 0
3 1320 提醒内容3 1 2007-12-13 11:08 0
4 1323 提醒内容4 1 2007-12-13 11:08 0
5 1326 提醒内容5 1 2007-12-13 11:09 0
Remind_Target:
REMIND_TARGET_ID(这个Id没有用) REMIND_INFO_ID LOGIN_ID
1 1316 1315 man111
2 1317 1315 man222
3 1335 1318 man222
4 1321 1320 man111
5 1322 1320 man222
6 1324 1323 man111
7 1325 1323 man222
8 1327 1326 man111
9 1328 1326 man222
Read_Remind:
READ_REMIND_ID(这个Id没有用) CLOSE_FLAG REMIND_RESOURCE_TYPE_ID REMIND_ENTITY_ID LOGIN_ID AFTER_REMIND_TIME(下次提醒时间,如果有值对应的CLOSE_FLAG是0。如果CLOSE_FLAG是1,记录关闭没有下次提醒时间)
1 1330 1 1 1315 xuyz
2 1337 0 1 1318 xuyz 2007-12-14
and (
to_number((select count(*) from Remind_Target rt where ri.remind_Info_Id = rt.remind_Info_Id))
>
to_number((select count(*) from Read_Remind readR where ri.remind_Info_Id = readR.remind_Entity_Id and readR.remind_Resource_Type_Id = 1 and readR.close_Flag = 1 ))
)
和
and (
(select count(*) from Remind_Target rt where ri.remind_Info_Id = rt.remind_Info_Id)
>
(select count(*) from Read_Remind readR where ri.remind_Info_Id = readR.remind_Entity_Id and readR.remind_Resource_Type_Id = 1 and readR.close_Flag = 1 ))
作为检索条件,检索的记录不一样。
你把你的建表的语句写出来,测试的几条数据,也用INSERT写出来。
把你的两次SQL也写出来,大家看看呢?得到什么结果才是正确的。什么是不正确的。还有,怎么都觉得你没有to_number的句子的括号不对称,多了一个。还有你的数据库的版本,都是什么。