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 7193729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:13:45+00:00 2026-05-28T20:13:45+00:00

I’m using MySQL 4.2, and I’m have 2 tables: tbl_User stores USERID and REGCODE.

  • 0

I’m using MySQL 4.2, and I’m have 2 tables:

tbl_User stores USERID and REGCODE. REGCODE is the code user used for signup the service.
tbl_Message stores messages posted by each of those users.

I need to query all users in tbl_User for the corresponding REGCODE to get total. Then, I would like to know is how many of these users have at least one entry in tbl_Message where their USERID matches up with AUTHORID. The last row is the % of user has post.

My goal is to calculate the percentage from two tables at last row, but I am not sure how to join tables in query to get the correct answer.

Tables chart: http://img526.imageshack.us/img526/6105/tablep.png

I asked this question yesterday, and @mathematical.coffee told me a simple query to get the result I want. It worked at first, but I found the query some issue with COUNT(tbl_User.USERID) and left join after I accepted the answer.

As you can see from this query, “COUNT(tbl_User.USERID) as totalUsers” is affected by the left join condition, thus the number of total users is wrong.
Can someone help me this, thanks so much!

SELECT REGCODE,
       COUNT(tbl_User.USERID) as totalUsers, 
       COUNT(tbl_Message.MESSAGEID) as usersWhoPosted 
       COUNT(tbl_Message.MESSAGEID)/COUNT(tbl_User.USERID)*100 As Percent
FROM tbl_User 
LEFT JOIN tbl_Message ON tbl_User.USERID=tbl_Message.AUTHORID 
WHERE REGCODE BETWEEN 9001 AND 9008
GROUP BY REGCODE

Result:

REGCODE   totalUsers  usersWhoPosted  Percent
9001      763         233             30.5374
...   
9008      345         235             68.1159
  • 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-05-28T20:13:47+00:00Added an answer on May 28, 2026 at 8:13 pm

    You can modify your query to use DISTINCT. The usersWhoPosted count needs modification, too. Your query is counting messages posted, not users who posted:

    SELECT 
        REGCODE,
        COUNT(DISTINCT tbl_User.USERID) AS totalUsers, 
        COUNT(DISTINCT tbl_Message.AUTHORID) AS usersWhoPosted 
        COUNT(DISTINCT tbl_Message.AUTHORID)/COUNT(DISTINCT tbl_User.USERID) * 100 
          AS Percent
    FROM tbl_User 
       LEFT JOIN tbl_Message 
         ON tbl_User.USERID = tbl_Message.AUTHORID 
    WHERE REGCODE BETWEEN 9001 AND 9008
    GROUP BY REGCODE
    

    You can also rewrite it like this:

    SELECT 
        REGCODE,
        COUNT(*) AS totalUsers, 
        COUNT(HasPosted) AS usersWhoPosted 
        COUNT(HasPosted)/COUNT(*) * 100 
          AS Percent
    FROM tbl_User 
       LEFT JOIN (SELECT 1 AS HasPosted) AS dummy 
         ON EXISTS
            ( SELECT *
              FROM tbl_Message
              WHERE tbl_User.USERID = tbl_Message.AUTHORID
            ) 
    WHERE REGCODE BETWEEN 9001 AND 9008
    GROUP BY REGCODE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the

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.