【问题标题】:how to count values of coulmn of tables如何计算表格列的值
【发布时间】:2017-04-30 23:33:08
【问题描述】:

我有两个表如下:

tbl_employee:

id

full_name

tbl_performance:

id

emp_id(fk)

innovator   (here emp_id will come)

outstanding_performer (here emp_id will come)

现在我想使用mysql查询,不使用任何子查询,从上面的表格中统计所有创新员工和优秀员工。

提前致谢!

【问题讨论】:

  • 你的意思是记录数?
  • 首先解释一下为什么tbl_performance表中emp_id有三列。该表的目的和数据是什么
  • 想用投票面板统计有多少员工是创新者,有多少表现突出
  • 您只能在一个列中执行此操作(可以命名为例如 special_employees)并为其设置标志,如 1:创新者,2:杰出_执行者。这对你来说会更好,而不是为同一种事物设置两个不同的列
  • 没有表已经在工作并且没有很好的记录

标签: php mysql mysqli


【解决方案1】:

您可能只需要条件聚合。

MariaDB [sandbox]> drop table if exists tbl_performance;
Query OK, 0 rows affected (0.11 sec)

MariaDB [sandbox]> create table tbl_performance (id int,emp_id int, innovator int, oustanding_performer int);
Query OK, 0 rows affected (0.32 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> insert into tbl_performance values
    -> (1,1,1,1), (2,2,2,null),(3,3,null,3);
Query OK, 3 rows affected (0.03 sec)
Records: 3  Duplicates: 0  Warnings: 0

MariaDB [sandbox]> select emp_id,
    -> sum(case when innovator is not null then 1 else 0 end) innovators,
    -> sum(case when oustanding_performer is not null then 1 else 0 end) oustanding_performers
    -> from tbl_performance
    -> group by emp_id with rollup;
+--------+------------+-----------------------+
| emp_id | innovators | oustanding_performers |
+--------+------------+-----------------------+
|      1 |          1 |                     1 |
|      2 |          1 |                     0 |
|      3 |          0 |                     1 |
|   NULL |          2 |                     2 |
+--------+------------+-----------------------+
4 rows in set (0.00 sec)

【讨论】:

  • 如果column默认值为0“not null”我们怎么办
  • 更改不为空为 0
  • 最好发布您期望的结果,因为我完全不清楚您在寻找什么。
  • 有多少创新者和杰出人士计算员工数据或明智地计算员工人数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 2019-02-22
相关资源
最近更新 更多