不要显示计数是否<2

时间:2014-12-25 13:38:49

标签: mysql sql

我试图隐藏并且只是在Working_On属性中显示它们,如果它们低于2.我尝试使用WHERE子句但没有运气。我是MySQL新手。一个人手?

这是我正在使用的表格:

mysql> SELECT* 
       FROM employee_project;


+------+-------+---------------+
| eNum | pCode | date_assigned |
+------+-------+---------------+
| E246 | P123  | 2014-12-05    |
| E246 | P294  | 2013-11-21    |
| E387 | P123  | 2014-12-20    |
| E403 | P123  | 2015-04-15    |
| E403 | P332  | 2014-04-01    |
| E933 | P294  | 2013-11-21    |
| E933 | P332  | 2014-03-01    |
| E933 | P342  | 2014-12-20    |
+------+-------+---------------+



mysql> SELECT eNum, COUNT(pCOde) as Working_On 
       FROM employee_project 
       GROUP BY eNum;


    +------+------------+
    | eNum | Working_On |
    +------+------------+
    | E246 |          2 |
    | E387 |          1 |
    | E403 |          2 |
    | E933 |          3 |
    +------+------------+
    4 rows in set (0.00 sec)

4 个答案:

答案 0 :(得分:4)

以下查询将为您提供帮助

SELECT eNum, COUNT(pCOde) as Working_On 
   FROM employee_project 
   GROUP BY eNum;
   HAVING COUNT(pCOde) < 2; 

答案 1 :(得分:2)

对此:

SELECT eNum, COUNT(pCOde) as Working_On 
   FROM employee_project 
   GROUP BY eNum;

添加:

having count(pCode) < 2

在group by子句之后。一些数据库引擎允许您这样做:

having working_on < 2

但我不知道mySql是否是其中之一。

答案 2 :(得分:2)

您只需要添加总是在聚合函数上执行的HAVING子句:

   SELECT eNum, COUNT(pCOde) as Working_On 
   FROM employee_project 
   GROUP BY eNum;
   HAVING COUNT(pCode)<2

答案 3 :(得分:1)

你可以使用这样的

SELECT eNum, COUNT(pCOde) as Working_On 
       FROM employee_project 
       GROUP BY eNum having count(pCode) < 2;

MySQL HAVING子句通常与GROUP BY子句一起使用。与GROUP BY子句一起使用时,可以将过滤条件应用于GROUP BY子句中显示的列。如果省略GROUP BY子句,则MySQL HAVING子句的行为类似于WHERE子句。请注意,MySQL HAVING子句将条件应用于每组行,而WHERE子句将条件应用于每个行。

了解更多信息,请阅读http://www.mysqltutorial.org/mysql-having.aspx