Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server
Hi,
I have a table Named ProductLike(ProductId, UserId, Like). Whenever user likes a product then a record is added in this table with UserId, ProductId(which product is liked) and Like(true or false).
I would like to get such usersId from the ProductLike table who has liked one product. To be more precise, I want to get a list or group of users who has liked same product for each product.

Please guide How can I write a query.

I am writing this one but it has errors:
select p.UserId,* from ProductLike p where p.ProductId = (select pk.ProductId from ProductLike pk
where pk.ProductLike=1)
Please guide as my query is not a solution it is just hint.
Posted 11-Jan-13 10:09am
Edited 11-Jan-13 10:30am
v5
Comments
Mika Wendelius at 11-Jan-13 15:15pm
   
Can you write a small example of the data and the desired output?
touseef4pk at 11-Jan-13 15:36pm
   
ProductId UserId Like
1 2 True
2 3 True
1 4 True
1 1 False

2 2 True
This is my my example data. Now for example productid 1 is liked by two users. I want that list/group of users for every product
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

SELECT ProductId, UserId
FROM ProductLike
WHERE [Like] = 1
ORDER BY ProductId, UserId

If you want a list for a specific product (for example ProductId 1) add
AND ProductId = 1
to the WHERE clause.

Please note that Like is a reserved keyword[^] and you might want to use another name for that field.
  Permalink  
v4
Comments
Maciej Los at 11-Jan-13 17:17pm
   
Complete answer, +5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Not sure if I understood the question correctly, but you mentioned group. So if you need each product and how many users liked it, the query could be something like:
SELECT pl.ProductId,
       COUNT(*) AS NumberOfLikes
FROM   ProductLike pl
WHERE pl.[Like] = True
GROUP BY pl.ProductId
  Permalink  
Comments
Maciej Los at 11-Jan-13 17:18pm
   
My favorite answer! +5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Not sure if I'm missing somethin in your question, but I would think this would do the job. You'll get a result set that contains all the products in sections.
SELECT ProductId, UserId
  FROM ProductLike
 WHERE Like = 1
 ORDER BY ProductId, UserId
  Permalink  
v2
Comments
Maciej Los at 11-Jan-13 17:17pm
   
I prefer the 3. solution ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 165
1 Sergey Alexandrovich Kryukov 134
2 Sebastiaan Meijerink 131
3 Rana Waqas 110
4 Maciej Los 84
0 Sergey Alexandrovich Kryukov 6,558
1 OriginalGriff 6,288
2 Peter Leow 2,534
3 Abhinav S 2,358
4 Maciej Los 2,352


Advertise | Privacy | Mobile
Web03 | 2.8.150414.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100