Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

sql join with junction table and normal table together on sql server

$
0
0
I am going to build an app similar to twitter. I have three tables. user, userprofile and followers.

user -> id, username, email, password, active 
userprofile -> id, userid, firstname, middlename, lastname, dob
followers-> id, userid, followingid


I wrote the below sql to search users. It also needs to return whether the logged in user is following the search result users.

  ALTER PROCEDURE	[dbo].[sp_User_Search]
    	@q nvarchar(250),
    	@userid uniqueidentifier
    AS
    BEGIN
    	DECLARE @p nvarchar(250)
    	set @p = '%' + @q + '%'
    	SELECT username,firstname,middlename,lastname,followingid
    	FROM [dbo].[user]
    	LEFT OUTER JOIN [dbo].[userprofile] ON [dbo].[user].id = [dbo].[userprofile].userid
    	LEFT OUTER JOIN [dbo].[followers] ON [dbo].[user].id = [dbo].[followers].followingid
    	WHERE (username like	@p or firstname like @p or middlename like @p or lastname like @p ) AND [dbo].[followers].userid = @userid
    END

But this didn't worked.Please help. Thanks in advance. 


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>