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

  • Home
  • SEARCH
  • 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 3615102
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:19:54+00:00 2026-05-18T22:19:54+00:00

Please help. I’m using MySQL 5.1.30 Community Edition. I have four tables: nts, operator,

  • 0

Please help. I’m using MySQL 5.1.30 Community Edition.

I have four tables: nts, operator, country, cooperationtype

  • table `nts` has one column(`operatorId`) which is a foreign key to column `id` in table `operator` and one column(`voice`) which is a foreign key to column `id` in table cooperationtype
  • table operator has one column(`country_id`) which is a foreign key to column (`id`) in table country

I want to get the counts of operators and countries where all of the value of voice not equals to ‘N/A’ and grouped them by cooperationtype.id with this query:

SELECT cooperationtype.id AS cooptype,
COUNT(DISTINCT country_id) AS country, COUNT(DISTINCT operatorId) AS operator
FROM nts INNER JOIN operator ON operator.id = nts.operatorId INNER JOIN country ON operator.country_id = country.id
INNER JOIN cooperationtype ON cooperationtype.id = nts.voice
WHERE cooperationtype.code <> 'N/A' GROUP BY cooperationtype.id

I got this result:

cooptype   country    operator
1         128          348
2         11           11
3         15           17

The sum of this query is 154 countries and 376 operators.

But then when I want to get all of the counts of operators and countries where all of the value of voice not equals to ‘N/A’, regardless the of cooperationtype.id with this query:

SELECT COUNT(DISTINCT country_id) AS country, COUNT(DISTINCT operatorId) AS operator
FROM nts INNER JOIN operator ON operator.id = nts.operatorId INNER JOIN country ON operator.country_id = country.id
INNER JOIN cooperationtype ON cooperationtype.id = nts.voice
WHERE cooperationtype.code <> 'N/A'

I got this result:

country operator
133     372

My questions are:

  1. Why is the sum of the result from the first query doesn’t equal to the result from the second query?
  2. Which one is the right result?

Data example:

voice     country   operator
1         US        1  
1         US        2  
1         UK        3  
1         UK        4  
2         US        1  
2         US        2  

For the first query, the data should generate:

cooptype  country   operator 
1         2         4 
2         2         2 

For the second query, the data should generate:

country   operator 
2         4 
  • 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-18T22:19:55+00:00Added an answer on May 18, 2026 at 10:19 pm

    Why is the sum of the result from the first query doesn’t equal to the result from the second query?

    Because you use COUNT(DISTINCT).

    It counts distinct records group-wise.

    Your first query counts two records with the same country but different cooptype twice (since it groups by cooptype), while the second one counts them once.

    Which one is the right result?

    Both are right.

    For the given data:

    cooptype  country
    1         US
    1         US
    1         UK
    1         UK
    2         US
    2         US
    

    the first query will return:

    1         2
    2         1
    

    and the second will return

    2
    

    , since you have:

    • 2 distinct countries in cooptype = 1 (US and UK)
    • 1 distinct country in cooptype = 2 (US)
    • 2 distinct countries overall (US and UK)

    Which is “right” in your definition of “right”, depends, well, on this definition.

    If you just want the second query to match the results of the first one, use

    SELECT  COUNT(DISTINCT cootype, country_id) AS country,
            COUNT(DISTINCT cooptype, operatorId) AS operator
    FROM    nts
    INNER JOIN
            operator
    ON      operator.id = nts.operatorId
    INNER JOIN
            country
    ON      operator.country_id = country.id
    INNER JOIN
            cooperationtype
    ON      cooperationtype.id = nts.voice
    WHERE   cooperationtype.code <> 'N/A'
    

    but, again, this may be as wrong as your first query is.

    For these data:

    cooptype  country  operator
    1         US       1
    1         US       1
    1         UK       2
    1         UK       2
    2         US       1
    2         US       1
    

    , what would be a correct resultset?

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

Sidebar

Related Questions

Please help me on this problem. I have 2 identical tables, one is timetable
Please help. I have the following - ListView and its two handlers AdminUsersListView_ItemEditing, AdminUsersListView_Load.
Please help me to find out how to allow mysql connector j to define
please help me out from this I have created a database in Blackberry phone
Please help in writing a code for conversion of source data in dd/mm/yyyy format.
Please help me solve this file path conflict: As you know, many HTML pages
Please help, how can I organize the process-process data exchange (in Windows, if that
Please help me get to the bottom of this...the only file that this error
please help on this issue: Within a loop, I first call a function, which
Please help me with the above question. An algorithm with a working example will

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.