matlaf wrote:
Here the code for the exists. How can I join the subquery to the parent query to have a query like :
select * from role where not exists (select null from user_role where user_role.id = role.id and user_role.id = 1)
Dim compare As ORMSupportClasses.FieldCompareSetPredicate
compare = New ORMSupportClasses.FieldCompareSetPredicate
(HelperClasses.RoleFields.RoleId, Nothing, _
HelperClasses.RoleUtilisateurFields.RoleId, Nothing,
ORMSupportClasses.SetOperator.Exist, Nothing)
compare.Negate = True
compare.SetFilter = HelperClasses.RoleUtilisateurFields.UtilisateurId = utilisateurId
I add compare to a RelationPredicateBucket here.
You've to set the objectalias of the set field in the set predicate, so you create a co-related subquery as it is called.
For the IN I didn't test it with oracle but with MYSQL this two queries don't return same resultset
select * from a where a.id not in (select b.id from b)
select * from a where not a.id in (select b.id from b)
Odd, as semantically, they represent the same thing. On sqlserver they give the same resultset.
Also earlier you said the documentation was wrong about EXISTS, it's not, as both require a co-related subquery if you want the subquery to work in that context.
In the oracle sql reference the not is between the expression and the subquery.
http://www.lc.leidenuniv.nl/awcourse/oracle/server.920/a96540/conditions5a.htm#1013449
Ok, though it's the same thing: A isn't part of subset B vs. NOT (A = part of subset B).
(For your information, NOT IN operator is not equal to <> ANY but is equal to <> ALL)
NOT equal ANY means not equal all. IN (...) is the same as equal any(...). NOT IN (...) means the same as NOT EQUAL ANY(...), as that expression is true if there's no pair (x, y), where x is part of the subset and Y is the value compared.
Or I miss something/am not thinking straight.
Either way: set the objectalias of the set field to alias the table in the subquery, and then add a predicate to the subquery which produces the filter to bind the subquery to the main query, so something like:
SELECT *
FROM Foo
WHERE Foo.A IN (SELECT A From Bar B where B.X = Foo.X)
this then can be written as:
SELECT *
FROM Foo
WHERE EXISTS
(SELECT * FROM Bar B where B.X = Foo.X)
I also would like to add:
select * from a where a.id not in (select b.id from b)
select * from a where not a.id in (select b.id from b)
they give the same resultset if the subquery is the same. So if I do:
SELECT * FROM Orders WHERE CustomerID NOT IN (select customerid from customers where country='Germany')
then I get the same resultset (and execution plan) as when I do:
SELECT * FROM Orders WHERE NOT CustomerID IN (select customerid from customers where country='Germany')
namely all orders of customers who don't live in germany.
I didn't add a NOT IN operator, as it would be redundant due to this reason. NOT field IN (...) is a perfectly correct expression, it's true when field IN (...) is false. which is the same as when field NOT IN (...) is true.