Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9059615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:01:21+00:00 2026-06-16T15:01:21+00:00

I am using PHP , MySQL for the following I have two tables users

  • 0

I am using PHP , MySQL for the following
I have two tables

  1. users
  2. users_messages

The table “users” stores records of all users .

id  name  password
1   XYZ   XYZ

[Here id is actually the user_id]

The table “messages” contains messages posted by users

id  user_id messages
1   1       XYZ

The “users” table contains thousands of records (>5000)
The “users_messages” contains about 100 records (will keep on varying)

I am trying to get the no of messages of all the users and show them in a table .
Currently i am trying to paginate them so i need only 20 users at one page load.

I have my query as follows

SELECT u.username as username,u.id as id, COUNT( f.user_id ) AS no_of_messages 
FROM users AS u
LEFT JOIN users_messages AS f ON f.user_id = u.id
GROUP BY u.id LIMIT 20

This works , but it takes a long time … may be it scans the complete table just to get the no of messages of only 20 users .

The id column of table “users” does not start from 1 , the start is random , but it auto increments from the next record .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:01:22+00:00Added an answer on June 16, 2026 at 3:01 pm

    The id column of table “users” does not start from 1 , the start is
    random

    Try this instead:

    SELECT 
      u.username as username, 
      u.id as id, 
      f.no_of_messages 
    FROM users AS u
    LEFT JOIN
    (
       SELECT user_id, COUNT(*) no_of_messages 
       FROM users_messages
       GROUP BY user_id
    ) AS f ON f.user_id = u.id
    ORDER BY u.id
    LIMIT 20;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using php and mysql I have two tables, a users table and a profiles
I'm using PHP/MySql. If I have two tables (hypothetically) as follows: id name and
using mysql and php, I have two tables, I'm french so the table names
I have following tables in MySQL.(I am using php/mysql) CREATE TABLE `hw_subjects` ( `id`
I'm using PHP and MySQL and have the following table structure: date | name
So I'm using PHP/MySQL and I have a tbl_profile table with the following fields:
I have the two following tables table A | id | name | |
I have two tables Schools and Users Table School ----------------------------------------------------- | id | name
On Windows using WAMPserver (Apache, MySQL, PHP) I have the following: //test.php if (!defined('LC_MESSAGES'))
Using PHP / MySQL all encoded up as UTF, we have recently had to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.