union - SQL query to find the friends in a table -
i have 2 tables, users
, friends
. tables like:
users | id | name | | 1 | sam | | 2 | harry | | 3 | vivi | | 4 | sabrina | friends | uid | friendid| | 1 | 2 | | 2 | 3 | | 4 | 1 | | 5 | 4 | | 1 | 3 |
i need find names of friends sam. tried doing same using union in sql query, couldn't desired output. can possibly required output doing same?
declare @answer nvarchar(max)='{' select @answer=@answer+u1.name+',' users u inner join friends f on f.uid=u.id inner join users u1 on u1.id=f.friendid u.id=<what ever want> //1 or 2 or 3 or 4 set @answer=substring(@answer,0,len(@answer)-1)+'}' select @answer
Comments
Post a Comment