two queries give different results in the same database
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:
- Why is the sum of the res开发者_开发问答ult from the first query doesn't equal to the result from the second query?
- 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 
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:
- 2distinct countries in- cooptype = 1(- USand- UK)
- 1distinct country in- cooptype = 2(- US)
- 2distinct countries overall (- USand- 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?
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论