Du kan bruge EXCEPT
operatør.
Førstegradskontakter:
SELECT contact_2 FROM contact WHERE contact_1 = 'gulsah'
Andengradskontakter, der ikke er førstegradskontakter:
SELECT
contactB.contact_2
FROM
contact AS contactB
INNER JOIN contact AS contactA ON contactA.contact_2=contactB.contact_1
WHERE contactA.contact_1 = 'gulsah'
EXCEPT
SELECT contact_2 FROM contact WHERE contact_1 = 'gulsah'
EXCEPT
fortæller SQL-serveren om at returnere alle resultater fra den første SELECT
som IKKE vises i den anden SELECT
.
For tredjegradskontakter (som ikke er første- eller andengradskontakter):
SELECT
contactC.contact_2
FROM
contact AS contactC
INNER JOIN contact AS contactB ON contactB.contact_2=contactC.contact_1
INNER JOIN contact AS contactA ON contactA.contact_2=contactB.contact_1
WHERE contactA.contact_1 = 'gulsah'
EXCEPT
(
SELECT contact_2 FROM contact WHERE contact_1 = 'gulsah'
UNION
SELECT
contactB.contact_2
FROM
contact AS contactB
INNER JOIN contact AS contactA ON contactA.contact_2=contactB.contact_1
WHERE contactA.contact_1 = 'gulsah'
)
Jeg har ikke store forhåbninger til ydeevne, men du skal selvfølgelig selv tjekke dette.
Som en sidebemærkning:
I can select mutual contacts but I guess it is not the right approach.
Brug INTERSECT
for dette.