选择“查询忽略字段”

时间:2012-09-06 16:06:45

标签: mysql sql

我有一个名为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    |
+-----------------------+----------+

2 个答案:

答案 0 :(得分:4)

试试这个,

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

SQLFiddle Demo

答案 1 :(得分:0)

select count(distinct(Name)) from test where operation != 'P' group by PROVIDER;