I'm having a little trouble here, and I'm not even sure if it's possible to do this without using a ton of queries. Hopefully you guys can guide me to a more optimized solution here.
Here's the table to start with. It's from a forum software.
|---------------------------------------------------------------------|
| id_msg | id_topic | id_board | id_member | poster_name | poster_time |
|---------------------------------------------------------------------|
What I need to do here is get a list of everyone who posted within a certain time (poster_name and poster_time) and then count how many times they posted in that time. The only way I can figure to do this is to first get the list:
SELECT id_member, poster_name
FROM messages
WHERE poster_time > UNIX_TIMESTAMP('some time')
AND poster_time < UNIX_TIMESTAMP('some other time');
And then loop through the people to find out how many times they posted and create the output.
foreach ($people as $person)
{
// Note that in real life, all the data is checked and sanitized.
$sql = '
SELECT COUNT(*) AS total
FROM messages
WHERE id_member = ', $person['id'], ';';
$list[] = $person['name'] . ': ' . $total;
}
It seems to me there should be a way to combine the queries and avoid the loop completely. It's not that big a deal if there are only a couple of people to loop through, but if there are a few hundred, this is nowhere near an optimized approach.