Check for a unique value within a count, but get all results

Posted by pedalpete on Stack Overflow See other posts from Stack Overflow or by pedalpete
Published on 2010-05-30T01:22:30Z Indexed on 2010/05/30 1:32 UTC
Read the original article Hit count: 290

Filed under:
|

I'm trying to create a single query which, similar to stack overflow, will give me the number of votes, but also make sure that the currently viewing user can't upvote again if they've already upvoted.

my query currently looks like

SELECT cid, text, COUNT(votes.parentid)  FROM comments LEFT JOIN votes ON comments.cid=votes.parentid AND votes.type=3 WHERE comments.type=0 AND comments.parentid='$commentParentid' GROUP BY comments.cid

But I'm completely stumpted on how to add the check to see if the userid is in the votes table.

The other option is to add a seperate query where

SELECT COUNT(*) FROM votes WHERE userid='$userid' AND parentid='$commentParentid' AND type=3

I'm just realizing I'm so lost with this that I don't even really know what tags to provide.

© Stack Overflow or respective owner

Related posts about mysql-query

Related posts about count