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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:31:17+00:00 2026-06-02T22:31:17+00:00

I am using the MySQL COUNT() method to find out how many rows in

  • 0

I am using the MySQL COUNT() method to find out how many rows in two tables have the uid of u.id.

To do so, I am using the method below:

SELECT u.id, u.first_name, u.last_name, u.email, u.username, COUNT( q.uid ) AS  `q_count`, COUNT( a.uid ) AS `a_count` 
FROM  `users` AS u
INNER JOIN `questions` AS q ON u.id = q.uid
INNER JOIN `answers` AS a ON u.id = a.uid
WHERE u.username =  'admin'

However, when I run the above query, the second count returns the same number of rows as the number of rows for the first count. I know this as the first count is returning two and the second is also doing the same, when there are two rows in the questions table and one row in the answers table.

Please can you tell me where I am going wrong?

Thanks


A sample of what I am receiving: http://d.pr/i/vcnJ

Sample data from answers: http://d.pr/i/TMkU

Sample data from questions: http://d.pr/i/tuwU

  • 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-02T22:31:19+00:00Added an answer on June 2, 2026 at 10:31 pm

    I believe the reason is that you’re doing a JOIN, which will JOIN the results together into one. Do use the same query but with SELECT * instead and you will see why this happens.

    Try this:

    SELECT u.id, u.first_name, u.last_name, u.email, u.username, COUNT( DISTINCT q.id ) AS  `q_count`, COUNT( DISTINCT a.id ) AS `a_count` 
    FROM  `users` AS u
    INNER JOIN `questions` AS q ON u.id = q.uid
    INNER JOIN `answers` AS a ON u.id = a.uid
    WHERE u.username =  'admin'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MySQL, I have three tables: projects : ID name 1 birthday party 2
Note: Using MySQL 4.0, which means no subqueries (at present). I have 2 tables:
Please help. I'm using MySQL 5.1.30 Community Edition. I have four tables: nts, operator,
I am using mysql. Here is my sql SELECT LIMIT 1 users.id, COUNT(events.id) AS
Using MySQL 5.x I want to efficiently select all rows from table X where
I'm using MySQL 4.2, and I'm have 2 tables: tbl_User stores USERID and REGCODE.
I'm using derived tables (latest version of MySQL) to get counts and data from
I'm looking for help using sum() in my SQL query: SELECT links.id, count(DISTINCT stats.id)
(Using MySQL and PHP) I have a search form that will allow my users
We're using MySQL with Innodb Engine storage. We have an evented environment that sends

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.