Assume these tables, with those fields:
History:
postID, viewerID
Posts:
postID, posterID
Now this query:
SELECT postID FROM history
WHERE postID IN (SELECT postID FROM Posts WHERE posterID IN (10,20,30)
AND viewerID=50
Will return all postIDS from posters 10,20 & 30, that viewer 50 has viewed.
What I’m after is a “group-by” that will be according to
the number of results of any posts per posterID.
For example, the results of the currently described query can have
100,200,300,300,300,100,400, 700, 500
Let’s say the first 7 results are from Poster 10 and the last 2 are from poster 20 –
So I need a result of:
posterID viewsNum
10 7
20 2
My SQL level is intermediate, so it is not immediately clear for me how to achieve this,
sorry if there’s a ‘simple’ answer hiding inside 🙂
What you need here, is just a
GROUP BYwith an aggregate functionCOUNT(), andJOINthe two tables instead. Something like: