Use GROUP BY and count. This will get you a list of user IDs to their counts:
SELECT intPosterId, COUNT(*)
FROM Posts
GROUP BY intPosterId
ORDER BY COUNT(*) DESC
LIMIT 20
You can use the result in a subquery:
SELECT u.cUsername, pcnt.postCount
FROM Users AS u
INNER JOIN (
SELECT intPosterId, COUNT(*) as postCount
FROM Posts
GROUP BY intPosterId
ORDER BY COUNT(*) DESC
LIMIT 20
) AS pcnt
ON u.intUserId = pcnt.intPosterId
To use it in a PHP script:
<?php
$sql = '
SELECT u.cUsername, pcnt.postCount
FROM Users AS u
INNER JOIN (
SELECT intPosterId, COUNT(*) as postCount
FROM Posts
GROUP BY intPosterId
ORDER BY COUNT(*) DESC
LIMIT 20
) AS pcnt
ON u.intUserId = pcnt.intPosterId
';
$pdo = new PDO(
'mysql:host=your_host;dbname=your_db',
'username',
'password',
array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION)
);
$stmt = $pdo->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "{$row['cUsername']}: {$row['postCount']} <br />\n";
}