计数和IN条款发布


Count and IN Clause Issue

我有一个名为employee history的表,其结构如下。

tbl_employee_history结构

id|history_nr|history_value|employee_id
1   82         83             1
2   86         84             1 
3   87         85             1
4   603                       1
5   82         83             2
6   86         83             2
7   87         83             2
8   603        83             2

这是我的表的伪数据。现在,我想对history_nr在82,86,87603中的所有员工进行计数,并且history_value不应该为空,就像上面的例子中一样,计数应该为1,因为对于员工id 2,所有值都不为空。这是我对实现计数的查询。

SELECT count(employee_id) FROM tbl_employee_history where history_nr IN(82,86,87,603) AND history_value!=''

但是,计数会返回两个值。提前谢谢。

删除查询的distinct()部分,这将只返回唯一的值。由于employee_id 1和2中各有4行,因此它将只获取具有不同(唯一)employee_id的第一行。

因此,它只提取2条记录,每个employee_id一条。

SELECT count(employee_id) FROM tbl_employee_history where history_nr IN(82,86,87,603) AND history_value != ''
//Returns 7 Rows
SELECT count(distinct(employee_id)) FROM tbl_employee_history where history_nr IN(82,86,87,603) AND history_value != ''
//Returns 2 Rows
相关文章:
  • 没有找到相关文章