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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:10:18+00:00 2026-06-07T22:10:18+00:00

I have about 5 tables with common field userid I want to count distinct

  • 0

I have about 5 tables with common field “userid” I want to count distinct userids across all these tables. Certain user ids may occur in about 2 of these tables. But I want to count the UNION of distinct user ids in the 5 tables together. I am able to count distinct userids in one table with the ff code. I want to be able count from all tables.

//just count the nuber of distict userids in table1
    $query2 = ("SELECT COUNT(DISTINCT userid) FROM table1"); 
    $result2 = mysql_query($query2) or die(mysql_error()); 
    $row2 = mysql_fetch_row($result2); 
    echo "Number of users in table 1  = ".$row2[0] ."<br />";


**Table 1**
Id    userid    name   adresss

**Table 2**
Id  Title   Sex   userid

**Table 3**
Id   userid   amount

**Table 4**
Id  price  promotion  userid   productid

**Table  5**
Id  userid   category   tax   weight
  • 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-07T22:10:19+00:00Added an answer on June 7, 2026 at 10:10 pm

    Use a UNION

    A standard UNION, in contrast with a UNION ALL, will remove duplicate values from the resultset. Your statement would look like

    SELECT   COUNT(*)
    FROM     (
               SELECT UserID FROM Table1
               UNION SELECT UserID FROM Table2
               UNION SELECT UserID FROM Table3
               UNION SELECT UserID FROM Table4
               UNION SELECT UserID FROM Table5
             ) AS UserIDs
    

    From w3schools

    the UNION operator selects only distinct values by default. To allow
    duplicate values, use UNION ALL.

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

Sidebar

Related Questions

I have about 100 tables where all of them have duplicate foreign key constraints
I have three tables with a common key field, and I need to join
I have two tables where I'm trying to select rows where a common field
I have a base table called users which holds all common information about a
I'm selecting data from two different tables which have a common field date and
I have a flow layout. Inside it I have about 900 tables. Each table
I have just learnt about temporary tables and using them has given me some
I have a database Student which contains about 20 tables. one of the tables
I have created a MySQL database using MySQL Workbench. It has about 20 tables.
i have two tables one is called addons and holds information about different addons,

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.