问题
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