Amandeep Singh
Amandeep Singh

Reputation: 11

How I can apply Inner Join to filter the data in Sql Server

I have table in which I inserted all the liked places records. Like: I have table PlaceLikes;

 Id    placeId    likedByUserID
  1      ABC          1
  2      DEF          1
  3      ABC          2
  4      FFF          2

Result: User 1 want to get all placeID that matches with itself.

  Id    placeId    likedByUserID
  3      ABC          2

Here User 2 with ABC placeId is similar with Requestor User ID 1. So, How I can filter the data like this

Upvotes: 0

Views: 49

Answers (2)

Abhijeet Kumar
Abhijeet Kumar

Reputation: 28

I think this piece of query will be able to solve your problem statement as understood by me.

DECLARE @sample_name VARCHAR (10);
SET @sample_name = 'placeId to be searched'
SELECT Id, placeId, likedByUserID FROM demo_table dt
WHERE dt.placeId = @sample_name

Do let me know if this was helpful.

Upvotes: 0

GMB
GMB

Reputation: 222582

You can use exists:

select t.*
from mytable t
where 
    t.likedByUserID <> 1
    and exists (
        select 1 from mytable t1 where t1.place_id = t.place_id and t1.likedByUserID = 1
    )

Upvotes: 1

Related Questions