Subject RE: [firebird-support] Subqueries from FB1.5 don't work in 2.0 or 2.1
Author Leyne, Sean
Paul,

> /* And now the offending query */
>
> SHELL ECHO This *should* display a single result (and does on FB1.5):;
>
> SELECT u.Id, u.Name, u.Email FROM Punter u WHERE u.ALIVE = 1
> /* they haven't blacklisted this issue */
> AND u.Id not IN (SELECT bl.UserId FROM IssueBlacklist bl WHERE bl.IssueId =
> 105)
>
> /* Subsribed via the product */
> AND u.id in (SELECT ps.UserId FROM ProductSubscription ps
> JOIN IssueProduct ip ON (ps.ProductId = ip.ProductId)
> WHERE ip.IssueId = 105 ) ;

Does this query work?

SELECT u.Id, u.Name, u.Email FROM Punter u WHERE u.ALIVE = 1
/* they haven't blacklisted this issue */
AND u.Id not IN (SELECT bl.UserId FROM IssueBlacklist bl WHERE bl.IssueId = 105)


From a strictly performance perspective, I am sure that you would find the following SQL to be much faster in execution (and it will work correctly for all FB versions):

SELECT u.Id, u.Name, u.Email
FROM Punter u
WHERE
u.ALIVE = 1
/* they haven't blacklisted this issue */
AND NOT EXISTS (
SELECT 1 FROM IssueBlacklist bl
WHERE
bl.UserID = u.Id and bl.IssueId = 105
)
/* Subsribed via the product */
AND EXISTS (
SELECT 1 FROM ProductSubscription ps
JOIN IssueProduct ip ON (ps.ProductId = ip.ProductId)
WHERE
ps.UserId = u.Id and ip.IssueId = 105
);