NOT EXISTS query doesn't work on Informix while same query with NOT IN works

倖福魔咒の 提交于 2019-12-12 02:26:58

问题


  select id from license where not exists(
  select a.id from license a,version b, mediapart c where
  c.version_id = b.id and b.cntnt_id = a.cntnt_id and c.is_highdef=1);

This query does not gives any rows in result set. Even on using different aliases for the outer license table and the inner one.

However, this one using NOT IN works fine:

  select id from license where id not in(
  select a.id from license a,version b, mediapart c where
  c.version_id = b.id and b.cntnt_id = a.cntnt_id and c.is_highdef=1);

Any suggestions on how can I achieve similar query with NOT EXISTS?

It's a home grown framework that I have to achieve this on and it won't be possible to write a query which is like following

 select id from license a where not exists(
 select a.id from version b, mediapart c where
 c.version_id = b.id and b.cntnt_id = a.cntnt_id and c.is_highdef=1);

The above query works but with the framework I am using, I will have to use all three table names and aliases in inner query.


回答1:


The NOT EXISTS query is correlated, but NOT IN is not. In other words the result of the subquery in the latter version is independent of the result of the main query.

I suspect there are records in license that have no children in version or mediapart, so they fall out of the correlated version of the query.

Without knowing more about the data design, I suggest you probably need to look at using an OUTER JOIN to ensure you get all the license records.



来源:https://stackoverflow.com/questions/24936904/not-exists-query-doesnt-work-on-informix-while-same-query-with-not-in-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!