how can I find the total records in this case:
I have a table ‘users’, a table ‘messages’ and a table ‘groups’. A user can sent a message, and that will be stored in the table ‘messages’. Each user can be a member of one group. Now I would like to show the total messages sent by all users in the group on the group-details-page. How can I get all the users in the group and count their messages? What is the best and fastest way to do that?
I can’t work with Joins, so this doesn’t work. I don’t know how to fix it.
SELECT COUNT(Message_id) AS total_messages, d.Userid FROM messages AS
d LEFT JOIN users AS s ON (s.Groupid=$groupid) WHERE
s.Groupid=$groupid
Thanks!
If you need table structure:
** users **
Userid
Groupid
** groups **
Groupid
Some_details
** messages **
Messageid
Userid
Subject
Content
If you’re looking for total messages without needing to know per user, try
Make sure you’re indexing the
messages.Useridfield as well if you’ll be doing a lot of querying based on it.Needing a count per user try