2

我有一个名为 test 的表,其中包含以下值。我想获取有多少用户正在使用开放 ID。

----+----------+-----------+
| NAME   | PROVIDER | OPERATION |
+--------+----------+-----------+
| Samuel | Google   | P         |
| Samuel | Google   | V         |
| Kannan | Google   | V         |
| George | Google   | V         |
| Bush   | Google   | V         |
| Bush   | Yahoo    | V         |
+--------+----------+-----------+

询问:select distinct(Name) from test group by PROVIDER

结果:

+-----------------------+----------+  
| count(distinct(NAME)) | PROVIDER |  
+-----------------------+----------+  
|                     4 | Google   |  
|                     1 | Yahoo    |  
+-----------------------+----------+

在此查询中,我想忽略PROVIDER的用户操作为P的计数。如何在单个查询中执行此操作?

输出应该是

+-----------------------+----------+
| count(distinct(NAME)) | PROVIDER |
+-----------------------+----------+
|                     3 | Google   |
|                     1 | Yahoo    |
+-----------------------+----------+
4

2 回答 2

4

试试这个,

SELECT  `Provider`, COUNT(DISTINCT Name)
FROM    tableName
WHERE   NAME NOT IN
            (
                SELECT Name
                FROM tableName
                WHERE Operation = 'P'
            )
GROUP BY Provider

SQLFiddle 演示

于 2012-09-06T16:09:23.813 回答
0
select count(distinct(Name)) from test where operation != 'P' group by PROVIDER;
于 2012-09-06T16:08:24.967 回答