Subject Re: [firebird-support] Firebird SQL Query question
Author Svein Erling Tysvaer
Hi Myles!

Simply write exactly what you said using SQL:

SELECT <whatever>
FROM USER_GROUP UG
WHERE NOT EXISTS(SELECT * FROM USER_GROUP_MEMBERSHIP UGM
WHERE UGM.UG_LINK = UG.LINK
AND UGM.UA_LINK = :UserAccount)

An alternative way, would be to write:

SELECT <whatever>
FROM USER_GROUP UG
LEFT JOIN USER_GROUP_MEMBERSHIP UGM
ON UGM.UG_LINK = UG.LINK
AND UGM.UA_LINK = :UserAccount
WHERE UGM.<PK> IS NULL

If both of these suggestions are too slow, write about it here, giving
lots more detail (table sizes, selectivity etc.)

HTH,
Set

myles@... wrote:
> I have a SQL query question that I'm hoping someone can point me in the
> right direction on.
>
> I have two main tables, and a join table. The tables are as follows:
>
> USER_ACCOUNT
> USER_GROUP
> USER_GROUP_MEMBERSHIP
>
> I need to find all the USER_GROUPS that a USER_ACCOUNT is NOT a member of.
>
> This is simply for each USER_GROUP that does not appear referenced in the
> USER_GROUP_MEMBERSHIP table for the USER_ACCOUNT.
>
> What is the best way to approach a query like this?
>
> Myles