Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
SQL
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
Updated 11-Jan-13 9:30am
v5
Comments
Wendelius 11-Jan-13 15:15pm    
Can you write a small example of the data and the desired output?
touseef4pk 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

SQL
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
SQL
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.
 
Share this answer
 
v4
Comments
Maciej Los 11-Jan-13 17:17pm    
Complete answer, +5!
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:
SQL
SELECT pl.ProductId,
       COUNT(*) AS NumberOfLikes
FROM   ProductLike pl
WHERE pl.[Like] = True
GROUP BY pl.ProductId
 
Share this answer
 
Comments
Maciej Los 11-Jan-13 17:18pm    
My favorite answer! +5!
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.
SQL
SELECT ProductId, UserId
  FROM ProductLike
 WHERE Like = 1
 ORDER BY ProductId, UserId
 
Share this answer
 
v2
Comments
Maciej Los 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900