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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:02:49+00:00 2026-06-14T21:02:49+00:00

I have a table called users which has info(first name,etc…) with the following rows:

  • 0

I have a table called “users” which has info(first name,etc…) with the following rows:

username
----------
mikha
guy
maricela

Another table called “questions” with the following rows:

asker
----------
mikha
mikha
maricela
guy
maricela

Another table called connections with the following rows:

username1
----------
guy
mikha
mikha

I want to select info about the user ‘mikha’ including count when he is questions.asker and when he is connections.username1. I use the following query:

SELECT COUNT(questions.asker) AS asking,COUNT(connections.username1) AS following 
FROM users LEFT JOIN questions ON users.username = questions.asker 
LEFT JOIN connections ON users.username = connections.username1 
WHERE users.username = 'mikha'

Expected results:

asking: 2 (as mikha is found 2 times asking)
following:  2 (as mikha is found 2 times following)

Actual results:

asking: 4
following: 4

If I use COUNT(DISTINCT questions.asker) and COUNT(DISTINCT connections.username1) I get the result always as 1 as it counts the name only once. I tried GROUP BY also with no avail.

So, how can I use distinct to avoid duplicates and the same time count the same name all the times available not just one?

I created this fiddle to test the problem yourself.

Thanks in advance
Regards
Michael

  • 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-14T21:02:50+00:00Added an answer on June 14, 2026 at 9:02 pm

    Do you have an ID in questions table, and one ID in connections table? If you have, you could use this:

    select
      users.username,
      count(distinct questions.id) as asking,
      count(distinct connections.id) as following
    from
      users left join questions on users.username = questions.asker
      left join connections on users.username = connections.username1
    where
      username='mikha'
    

    EDIT: based on your comments, i think that what you need is this:

    select
      COUNT(distinct questions.id) AS asking,
      COUNT(DISTINCT CONCAT(c1.username1,c1.username2)) AS following,
      COUNT(DISTINCT CONCAT(c2.username1,c2.username2)) AS followers
    FROM
      users LEFT JOIN questions ON questions.asker=users.username
      LEFT JOIN connections c1 ON c1.username1=users.username
      LEFT JOIN connections c2 ON c2.username2=users.username
    WHERE
      users.username = 'mikha'
    

    the only problem here is that counting by CONCAT(...) does not take advantage of indexes and can be slow. And also it could happen that CONCAT(userA, userB) = CONCAT(userC, userD) even if userA<>userC and userB<>userD. You could use CONCAT(username1, ':', username2) to avoid this, but only if you make sure that no username contains : character. But if it is possible, i would suggest you to add an id also to your connections table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a table called Users, which contains your typical information: Id, Name,
I have a very typical situation. We have a table called Users which has
Let's say I have a table called Users which represents registered users of a
I have a MySQL table called Users. The rows have 4 fields and look
I have a base table called users which holds all common information about a
Rails 3.0.3 ruby 1.9.2p0 The Problem: I have a Users table which has many
I have 1 table in my mysql database called clubusers which lists users emails
I have a MS Access table which has a column called recordLocked. Think of
I have a table called users which currently contains column money of type integer
I currently have a table called User which has a id column which is

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.