Reputation: 107
Do both the following queries always give the same results? If not, why not?
1)
select PayFrequencyTypeID, PayFrequencyDesc
from db_uspaybo.tblpayfrequency
where (PayFrequencyTypeID,1) not in (
select payfrequencytype,1
from tblcustomerpayapproval
where fedtaxid='903008887' and payrollyear=year(curdate())
);
2)
select payfrequencytypeid
from tblpayfrequency
where not exists (
select distinct payfrequencytype
from tblcustomerpayapproval
);
Thanks in advance.
Upvotes: 0
Views: 437
Reputation: 151
I think this is what you want with 'Not Exists' A.Id and B.Id here is the Pk and FK that connect these tables.(I don't know the exact field name)
select payfrequencytypeid from tblpayfrequency A
where not exists ( select *
from tblcustomerpayapproval B where A.Id=B.Id and B.fedtaxid='903008887' and B.payrollyear=year(curdate()));
Upvotes: 2
Reputation: 151
The first one returns the some data from db_uspaybo.tblpayfrequency where their PayFrequencyTypeID is not in the payfrequencytype table with those conditions.
The second one does not return any data from db_uspaybo.tblpayfrequency.
Upvotes: 1
Reputation: 36229
Simplified and tagged:
SELECT p, d
FROM f
WHERE (p, 1) NOT IN
(SELECT t, 1
FROM a
WHERE i='903008887'
AND y = year (curdate()));
SELECT p
FROM f
WHERE NOT EXISTS
(SELECT DISTINCT t
FROM a);
The result will not be the same. Reason: The first query asks for 2 columns. But we cannot know wheter the question for the year has some effect, or the filtering for 903008887. If that filtering has an effect - how could it be done by the second query?
Upvotes: 1
Reputation: 116110
Basically not in
and not exists
are very similar and usually yield the same result.
A difference is that in
will return false if one of the values in the set is NULL (at least, it does on Oracle), while exists
only checks for existance of a record, unregarding its values.
In this specific case, you got a WHERE clause that will cause the first query to return a different result.
A third approach which is generally faster on MySQL, is to left join the table in the main query and check if the join field is NULL:
select payfrequencytypeid
from
tblpayfrequency f
left join tblcustomerpayapproval a
on a.payfrequencytype = f.payfrequencytype
where
a.payfrequencytype IS NULL
Other general tips:
1
of course.Upvotes: 1
Reputation: 2335
Look at the following blog:
Alternatively you can always google for such type of questions.
Upvotes: 0