I have a table of node likes, which looks roughly like this:
lid nid uid type
1 23 3 like
2 23 1 like
3 49 3 dislike
4 11 6 like
lid = unique ID for this table, nid = “node” (content) ID, uid = user ID and type is self explanatory.
With this query:
SELECT nid, COUNT(lid) AS score, type
FROM node_likes
INNER JOIN users ON node_likes.uid = users.uid
GROUP BY nid, type
I can get each node with its like and dislike scores. The inner join is irrelevant; some (dis)likes are from users that no longer exist, and the join is to eliminate them.
The result looks like this:
nid score type
307 4 like
307 1 dislike
404 24 like
How can I then sub-group this query by type, and return the top-scoring node ID for each “like” type (like/dislike)?
Ie.
nid score type
404 24 like
307 1 dislike
1 Answer