2016-04-16 32 views
0

我有兩個表employee_info和考勤:連接兩個表來獲得出席和用戶名的mysql

CREATE TABLE IF NOT EXISTS `employee_info` (
    `emp_id` varchar(20) NOT NULL, 
    `emp_name` varchar(30) NOT NULL, 
    `emp_contact` varchar(30) NOT NULL, 
    `com_address` varchar(30) NOT NULL, 
    `per_address` varchar(30) NOT NULL, 
    `com_phone` varchar(13) NOT NULL, 
    `com_email` varchar(20) NOT NULL, 
    `empid` int(20) NOT NULL, 
    `emp_company` varchar(20) NOT NULL, 
    `emp_branch` varchar(20) NOT NULL, 
    `emp_dept` varchar(20) NOT NULL, 
    `emp_designation` varchar(20) NOT NULL, 
    `emp_salary` varchar(30) NOT NULL, 
    `emp_type` varchar(10) NOT NULL, 
    `last_updated` date NOT NULL, 
    `active` int(11) NOT NULL 
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1; 

CREATE TABLE IF NOT EXISTS `attendance` (
`id` int(11) NOT NULL, 
    `company` varchar(20) NOT NULL, 
    `branch` varchar(55) NOT NULL, 
    `dept` varchar(100) NOT NULL, 
    `employee_id` varchar(255) NOT NULL, 
    `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `status` varchar(12) NOT NULL 
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1; 

我得到的總總在場與不在場計數使用此查詢:

select employee_id 
    ,count(case when status =1 then 1 end) as absent_count 
    ,count(case when status =2 then 1 end) as present_count 
    ,count(distinct time) as Tot_count 
from attendance where time between '2014-01-01' and '2016-04-12' 
group by employee_id 

我怎樣才能加入這兩個表來獲得emp_name ???

回答

0
select employee_id, 
    count(case when status =1 then 1 end) as absent_count, 
    count(case when status =2 then 1 end) as present_count, 
    count(distinct time) as Tot_count 
from attendance 
    join employee_info on attendance.employee_id = employee_info.id 
where time between '2014-01-01' and '2016-04-12' 
group by employee_id 
+0

我編輯它作爲我的要求,它works.thank you.select EMPLOYEE_ID,EMP_NAME,計數(情況下,當狀態= 1個,則1端)作爲absent_count,計數(情況下,當狀態= 2則1端)作爲present_count,作爲Tot_count從考勤加入employee_info on attendance.employee_id = employee_info.empid中計數(不同時間),其中'2014-01-01'和'2016-04-13'組之間的時間由employee_id – Jannat

+0

非常歡迎:) – KnowHoper

+0

請問另請幫忙? – Jannat