开发者

using or in mysql and merging columns

开发者 https://www.devze.com 2022-12-28 02:00 出处:网络
I can select using or with the s开发者_如何学Goql statement select f.userid, f.friend_userid from friends f where userid = 1 or friend_userid = 1;

I can select using or with the s开发者_如何学Goql 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


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消