So I have this query that pulls from my links and votes table and I need one last column of data. My votes table consists of every user’s vote, a user can only vote once per link and their vote value is either -1, 0 or 1. There is a user_id foreign key in the votes table and I want to somehow gather the current user’s vote. I feel the complexity of the current query might require a second query but I really want to avoid that if possible. I simply need to know what the current logged in user’s vote was. A link will never have more than one vote value because a user can only vote on a link once.
A few notes
- All links start automatically with at least one vote entry by the current user
- A user that votes on a link then deselects that vote will keep that vote entry with a 0 delta
SQL:
SELECT links.*, (SUM(votes.karma_delta)) AS karma
FROM links, votes
WHERE links.id = votes.link_id
GROUP BY votes.link_id
ORDER BY (SUM(votes.karma_delta) - 1) / POW((TIMESTAMPDIFF(HOUR, links.created, NOW()) + 2), 1.5) DESC
LIMIT 0, 100
While optimization is great, right now I just want to get the selected links karma_delta by a specified user.
I’m really not sure of what you’re asking, but it sounds like you want to keep the information you’re already returning and simply augment it with the sum of votes pertaining to the current user for each link.
If that’s the case, then something like this should do it: