Q:

In MySQL how do I count messages from a user?

I have tables as below.
Table Messages
message_id
parent_id
forum_id
user_id

Table Users
user_id
username
pass

How to query a user, and display the count of their messages with their username?
My query:
select count(subject), user_id from messages group by user_id;
The problem is that it only displays user_id and count of messages but no username.  How do I make it display the name of the user?

A:

You need to join the users table into your query:
select count(*), username
from messages, users
where users.user_id = messages.user_id
group by users.user_id;

A:

I think this is the query you want. 
$query = "SELECT u.username, COUNT(*) as total_count FROM messages m INNER JOIN users u ON m.user_id = u.user_id GROUP BY U.user_id"

Here total_count gives the total count of messages grouped by user id to avoid joining of same usernames.

