我正在尝试在嵌套中执行两个查询,但下面的查询没有给出任何o / p
select Object_name from IM_top where Id_number in
(select t.Id_number from IM_top t,IM_alarm_state a,IM_network_element n,Related_to_AH_id r
where t.Id_number = a.Id_number and t.Id_number = n.Id_number and t.Id_number *= r.Id_from
and t.Object_class like 'IM_top/IM_alarm_state/IM_network_element%') where Object_name not in (select substring(object,LEN(object)-CHARINDEX(',',REVERSE(object)) + 2,CHARINDEX(',',REVERSE(object)) + 1 ) from fmadb_1_1..FMA_alarm_text_route )
当我单独运行这些查询时,第一个查询返回2206行
select Object_name from IM_top where Id_number in
(select t.Id_number from IM_top t,IM_alarm_state a,IM_network_element n,Related_to_AH_id r
where t.Id_number = a.Id_number and t.Id_number = n.Id_number and t.Id_number *= r.Id_from
and t.Object_class like 'IM_top/IM_alarm_state/IM_network_element%')
,第二个查询返回184行
select substring(object,LEN(object)-CHARINDEX(',',REVERSE(object)) + 2,CHARINDEX(',',REVERSE(object)) + 1 ) from fmadb_1_1..FMA_alarm_text_route
我期待2206-184的差异,当我运行嵌套查询时它是2022行,但它给出的o / p是0行。
任何解决方法?
答案 0 :(得分:1)
您的子查询返回NULL,这总是会产生一个空答案集:
a NOT IN(1,NULL)
在逻辑上等同于
a<> 1和a< 1> NULL
解析为
TRUE / FALSE AND UNKNOWN
最终导致
UNKNOWN
任何一行。
添加WHERE条件以删除NULL或更好地将NOT IN更改为NOT EXISTS。
答案 1 :(得分:0)
您使用了两个Where
条款而不是AND
试试这个
select Object_name from IM_top where Id_number in
(select t.Id_number from IM_top t,IM_alarm_state a,IM_network_element n,Related_to_AH_id r
where t.Id_number = a.Id_number and t.Id_number = n.Id_number and t.Id_number *= r.Id_from
and t.Object_class like 'IM_top/IM_alarm_state/IM_network_element%') AND Object_name not in (select substring(object,LEN(object)-CHARINDEX(',',REVERSE(object)) + 2,CHARINDEX(',',REVERSE(object)) + 1 ) from fmadb_1_1..FMA_alarm_text_route )
希望这能帮到你