> "Dieter Bender" wrote...
>
[quoted text clipped - 34 lines]
>
> // Bjorn A
"Alexey Shevchenko" wrote...
> "Bjorn Abelli" wrote...
>>>> Hi all
>>>> can anyone suggest an sql query for the following table...
[quoted text clipped - 14 lines]
>>>> For example Direct Friends of 1 must return the values 2,3,5,8.
>>>> So can u please suggest an SQL query for this??
>> select FndId as Friends
>> from TblFnds
[quoted text clipped - 5 lines]
>>
>> // Bjorn A
> Why we can not use the following:
>
> select FndId as Friends
> from TblFnds
> where ID = 1 or FndId = 1
You'll get the result: 2, 3, 5, 1, 1
...and not the intended: 2, 3, 5, 8
Look at what column you're retrieving values from: FndId
Then look again into my suggestion (above, with the union), and look at the
two separate queries inside.
Each of the queries retrieves values from one column depending on the value
in *another* column, then merges the results from those queries. As "union"
eliminates duplicates, 5 will only turn up once, as intended.
// Bjorn A
P.S. Please don't top post.
Alexey Shevchenko - 06 Mar 2006 15:55 GMT
Oh, yes - UNION ALL is acceptable. Of course, only if the data type of
columns is identical.
> "Alexey Shevchenko" wrote...
>
[quoted text clipped - 51 lines]
>
> P.S. Please don't top post.
Bjorn Abelli - 06 Mar 2006 22:51 GMT
"Alexey Shevchenko" wrote...
> "Bjorn Abelli" wrote...
>> "Alexey Shevchenko" wrote...
>>> "Bjorn Abelli" wrote...
>>>> select FndId as Friends
>>>> from TblFnds
[quoted text clipped - 22 lines]
>> value in *another* column, then merges the results from those queries. As
>> "union" eliminates duplicates, 5 will only turn up once, as intended.
> Oh, yes - UNION ALL is acceptable.
No, the *default* for UNION is to eliminate duplicates (as with DISTINCT),
but using the keyword ALL *keeps* them.
> Of course, only if the
> data type of columns is identical.
In this case they obviously are identical.
// Bjorn A
P.S. And I repeat the plead from the last message:
Please don't top post.
It just messes things up and make it difficult for people to follow any
discussion.