I have the following SQL statement by now:
SELECT m . *
FROM newsletter_mail_list m
INNER JOIN (
SELECT n.id
FROM newsletter n
GROUP BY n.customer_id
ORDER BY n.id ASC
) b ON m.newsletter_id = b.id
WHERE m.sent_date IS NULL
Mail List Table
+--------+-------+--------+--------+-----+
| id | newsletter_id | email | sent_date |
+--------+-------+--------+--------+-----+
| 1 | 8 | abcd | today |
| 2 | 8 | cfdf | NULL |
| 3 | 8 | afdg | NULL |
| 4 | 9 | zfbh | NULL |
| 5 | 9 | eerg | NULL |
| 6 | 9 | ertg | NULL |
| 7 | 9 | zfbh | NULL |
| 8 | 9 | eerg | NULL |
| 9 | 9 | ertg | NULL |
| 10 | 9 | zfbh | NULL |
| 11 | 9 | eerg | NULL |
| 12 | 9 | ertg | NULL |
| 13 | 9 | zfbh | NULL |
| 14 | 9 | eerg | NULL |
| 15 | 9 | ertg | NULL |
| 16 | 9 | zfbh | NULL |
| 17 | 9 | eerg | NULL |
| 18 | 9 | ertg | NULL |
+--------+-------+--------+--------+-----+
Desired Result
+--------+-------+--------+--------+-----+
| id | newsletter_id | email | sent_date |
+--------+-------+--------+--------+-----+
| 2 | 8 | cfdf | NULL |
| 3 | 8 | afdg | NULL |
| 4 | 9 | zfbh | NULL |
| 5 | 9 | eerg | NULL |
| 6 | 9 | ertg | NULL |
| 7 | 9 | zfbh | NULL |
| 8 | 9 | eerg | NULL |
| 9 | 9 | ertg | NULL |
| 10 | 9 | zfbh | NULL |
| 11 | 9 | eerg | NULL |
| 12 | 9 | ertg | NULL |
| 13 | 9 | zfbh | NULL |
+--------+-------+--------+--------+-----+
This one gives me all mail_list rows, where the sent_date is NULL and the foreign newsletter is the oldest from the customer.
Now I only want maximal 10 mail_list rows returned FOR EACH newsletter returned by the subselect.
Example:
By now I'm getting 21 results by this SQL, 3 mail_lists that belong to the newsletter 8 and 18 results for the newsletter 9. But the number of results for the newsletter 9 should only be 10.
How could I do this with SQL? Is this even possible?