Here is my table looks..
Users
Id name height weight
1 aaa 1 10
2 bbb 4 104
3 ccc 1 10
4 ddd 56 150
5 eee 232 180
second table looks like
Profile view
Id sender receiver block
1 1 2 True
2 2 3 False
3 4 1 False
The problem i am facing is,,When I search using height and weight in users table and block using profileview table.I couldn't get proper results..
If second user bbb search with height "1" and weight "10" it should be appear 3rd user details ccc .First user also matched but First user blocked second user.The problem is when i using join values coming if sender and receiver exists i the profileview table.If not exist how do we do in joins..
CREATE TABLE #Users
(
Id int,
[Name] varchar(255),
Height int,
[Weight] int
)
CREATE TABLE #ProfileView
(
Id int,
Sender int,
Receiver int,
[Block] varchar(5)
)
INSERT INTO #Users
(Id, [Name], Height, [Weight])
VALUES
(1, 'aaa', 1, 10),
(2, 'bbb', 4, 104),
(3, 'ccc', 1, 10),
(4, 'ddd', 56, 150),
(5, 'eee', 232, 180)
INSERT INTO #ProfileView
(Id, Sender, Receiver, [Block])
VALUES
(1, 1, 2, 'True'),
(2, 2, 3, 'False'),
(3, 4, 1, 'False')
DECLARE
@CallingUser int, --User performing the search
@Height int, --Height searched for
@Weight int --Weight searched for
SET @CallingUser = 2
SET @Height = 1
SET @Weight = 10
SELECT
*
FROM
#Users
LEFT OUTER JOIN #ProfileView
ON #Users.Id = #ProfileView.Id
AND #ProfileView.Receiver = @CallingUser
WHERE
#Users.Id <> @CallingUser
AND #Users.Height = @Height
AND #Users.[Weight] = @Weight
AND (#ProfileView.[Block] = 'False' OR #ProfileView.[Block] IS NULL)
DROP TABLE #Users
DROP TABLE #ProfileView