Two queries give different results in one database

Please, help. I am using MySQL 5.1.30 Community Edition.

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

  • table `nts` has one column (` operatorId`), which is the foreign key for the `id` column in the operator` table and one column (` voice`), which is the foreign key for the id column in the cooperationtype table
  • has one column (`country_id`), which is the foreign key for the column (` id`) in the table.

I want to get the calculations of operators and countries where all voice values ​​are not equal to "N / A" and are grouped by them with the cooperationtype.id team 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

- 154 376 .

, , "N/A", , :

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'

:

country operator
133     372

:

  • ?
  • ?

:

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

:

cooptype  country   operator 
1         2         4 
2         2         2 

:

country   operator 
2         4 
+3
1

?

COUNT(DISTINCT).

.

, cooptype ( cooptype), - .

?

.

:

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

:

1         2
2         1

2

:

  • 2 cooptype = 1 (US UK)
  • 1 cooptype = 2 (US)
  • 2 (US UK)

"" "", .

, ,

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'

, , , .

:

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

?

+3
source

Source: https://habr.com/ru/post/1782320/


All Articles