How would you write a MySQL query that will limit the results of a joined table (or sub select if that works better) and also counts the number of items in the joined table or tables?
For instance, let's say you had three tables: projects, tasks and comments, where a project has 0 or more tasks and a task has 0 or more comments. How would you limit the number of tasks returned per project to 3 and also return the total number of tasks per project and comments per task?
Here's what I imagine the result set look like:
project_id, project_title, task_id, task_title, num_tasks, num_comments
------------------------------------------------------------------------
1, Project1, 1, Task1, 4, 3
1, Project1, 2, Task2, 4, 0
1, Project1, 3, Task3, 4, 9
2, Project2, 10, Task10, 20, 0
2, Project2, 11, Task11, 20, 0
2, Project2, 12, Task12, 20, 2
3, Project3, 20, Task20, 17, 5
3, Project3, 21, Task21, 17, 1
3, Project3, 22, Task22, 17, 2
Where 'Project1', 'Project2', etc just represent a project's title and 'Task1', 'Task2', etc represent a task's title.
Ultimately, (after parsing through the results of the query) I'd like to be able to display something like this:
Project1 (4 tasks)
Task1 (3 comments)
Task2 (0 comments)
Task3 (9 comments)
Project2 (20 tasks)
Task10 (0 comments)
Task11 (0 comments)
Task12 (2 comments)
Project3 (17 tasks)
Task20 (5 comments)
Task21 (1 comments)
Task22 (2 comments)
I'm guessing this has to be done with sub selects (which is fine), but I can't seem to figure out how to accomplish this with just using joins and I don't quite have a good enough handle on sub selects to do something like this.