I got a table with threads and one with posts. I want to list these ordered by the number of post associated to them.
Example structure of my tables
Posts table:
id creator replyTo text timestamp
1 1 1 Bla 2011-11-11 11:11
2 2 2 Alb 2011-11-11 11:11
3 3 3 Lba 2011-11-11 11:11
4 4 1 Lab 2011-11-11 11:11
5 5 2 Bal 2011-11-11 11:11
6 2 2 Abl 2011-11-11 11:11
Threads table:
id creator name content timestamp
1 1 BlaBla BlaBla 2011-11-11 11:11
2 3 AlbAlb AlbAlb 2011-11-11 11:11
3 2 LbaLba LbaLab 2011-11-11 11:11
Example output:
id creator name count timestamp
2 3 AlbAlb 3 2011-11-11 11:11
1 1 BlaBla 2 2011-11-11 11:11
3 2 LbaLba 1 2011-11-11 11:11
Try this:
Produces exactly the requested results.
See the working demo here.
is short for (because it is enough to group by the primary key in mysql):
which is short for (because these are positional parameters):
Answer to additional question in comment
Add a
WHEREclause like this: