mysql - Getting Follower and FollwedBy Users from Table in one list -


i have table tracks followers

followeruserid, followinguserid 1                   2 2                   1 3                   1 4                   1 1                   5 

i want user given id follows , followed or both. example userid 1,i want result be: (fg: following, fd: followed, b: both ways)

2,b 5,fg 3,fd 4,fd 

i can fg , fd doing union

select followeruserid, 'fd' table followinguserid =1 union select followinguserid, 'fg' table followeruserid =1; 

with above user 2 as

2,fg  2,fd  

from above but need 2,b without userid 2 duplicated. how can done efficiently?

you can use aggregation on basic query:

select userid,        (case when count(distinct which) = 1 min(which)              else 'b'         end) (select followeruserid userid, 'fd' table followinguserid = 1       union       select followinguserid, 'fg' table followeruserid = 1      ) f group userid; 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -