tags:

views:

47

answers:

3

I can select using or with the sql statement

select f.userid, f.friend_userid from friends f where userid = 1 or friend_userid = 1;

now either userid or friend_userid is returning 1.

i want the two columns i.e userid and friend_userid to get merged

into a single column without 1 such that only one row is displayed...

the output i m getting is...

userid | friend_userid

1 | 2

1 | 7

1 | 5

12|1

24 | 1

I want to get displayed like...

userid

2

7

5

12

24

I m using mysql....

Thanks

Pradyut
India

+1  A: 

Looks like you want a join, probably a LEFT JOIN, between two instances of table friends. If the fields other than userid and friend_userid are, say, a and b (you don't tell us and it's impossible to guess:

SELECT f.a, f.b, f1.a, f1.b
FROM friends f
LEFT JOIN friends f` ON (f.userid = f1.friend_userid)
WHERE f.userid = 1
Alex Martelli
no i just want the user_id or the friend_userid only...but the output should not return 1 as in my case...i just edited the question...
Pradyut Bhattacharya
A: 

with the sql

 select f.userid, f.friend_userid from friends f where userid = 1 or friend_userid = 1;

the output i m getting is...

userid | friend_userid

1 | 2

1 | 7

1 | 5

12|1

24 | 1

I want to get displayed like...

userid

2

7

5

12

24


thanks
Pradyut

Pradyut Bhattacharya
A: 

So, you need the column friend_userid where condition "UserId=1" is met and UserId column when "friend_userid=1" condition is met. Write the following query:

select f.friend_userid as userId from friends f where f.userid = 1 
UNION
select f.userid as userId from friends f where f.friend_userid = 1;

And you get what you wanted.

Kangkan
great thanks for the help... i just sorted it out...and it was the same as your answer...thanks...
Pradyut Bhattacharya