如何计算表的库仑值

时间:2016-12-15 11:20:00

标签: php mysql mysqli

我有两张表:

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查询统计所有创新员工和优秀员工,而不使用任何子查询。

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可能只需要条件聚合。

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)