I’ve got the following SQL query. I want to get 5 posts from this query, but I also want to limit it so that I can, at most, get two posts per user. Only two users in this case, would mean a maximum of four posts.
SELECT DISTINCT *
FROM posts
WHERE (user_id IN (2,1000001)
AND NOT track_id = 34)
GROUP BY
track_id
ORDER BY
id desc LIMIT 5
Add a condition to your WHERE clause in which you count the number of rows for that user with a greater id, and make sure there are only 1 or 0 rows.