2016-12-15 60 views
-5

的coulmn值我有以下兩個表:怎麼算表

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查詢來計算上表中所有創新者員工和傑出表現者,而不使用任何子查詢。

在此先感謝!

+0

你的意思是記錄算不算? – Kulasangar

+0

首先解釋爲什麼在tbl_performance表中爲什麼有三列emp_id。目的是什麼,並在該表中都有哪些數據 –

+0

要統計有多少員工是創新和使用投票面板 – Anshul

回答

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) 
+0

我們做什麼,如果默認值列是0「非空」 – Anshul

+0

變化是不爲空,以<> 0 –

+0

記錄必須明智顯示emp_id爲 – Anshul