I have 2 tables, one has the categories within it, and the other has the distinct items in it.
What I want to do is to select
the item_id and cat_id, and limit it to 5 result for each category, for example:
cat_id item_id
1 1
1 2
1 3
1 4
2 5
2 6
3 7
etc…
The closest query i’ve come up with
SELECT cat.cat_id, cat.cat_name, item_id, item_author, item_name, item_pic_big
FROM item_table ipt
JOIN cat_table cat ON ipt.cat_id = cat.cat_id
GROUP BY item_id
HAVING COUNT(*) < 5
ORDER BY cat.cat_id
That’s what I found on stackoverflow, however if I want to change the count to let’s say… 2, it gives me the same result, and if I change the group by to cat_id, it only gives me 1 result.
Any help would be appreciated
What you actually need is something like:
Unfortunately, if you try to run this you will get this disappointing
error message:
So you’ll need a workaround. You can see a list of possible workarounds here:
http://www.artfulsoftware.com/infotree/queries.php#104
EDIT: The first solution there can be translated to your structure like this:
(I don’t have your tables, so there may be minor column names issues)