加入两张表获取考勤和用户名mysql
Posted
技术标签:
【中文标题】加入两张表获取考勤和用户名mysql【英文标题】:Join two table to get attendance and user name mysql 【发布时间】:2016-04-16 04:38:14 【问题描述】:我有两张表employee_info 和出席:
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???
【问题讨论】:
【参考方案1】: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
【讨论】:
我按照我的要求对其进行了编辑,它可以工作。谢谢。选择employee_id,emp_name,count(状态=1然后1结束的情况)作为absent_count,count(状态=2然后1结束的情况) as present_count ,count(distinct time) as Tot_count from Attendance join employee_info on admission.employee_id = employee_info.empid where time between '2014-01-01' and '2016-04-13' group by employee_id 我可以请其他人帮忙吗?? 当我尝试加入另一个表时approved_loan:选择approved_loan.employee_id,approved_loan.amount,emp_name ,emp_salary,count(case when status =1 then 1 end) as缺席_count,count(case when status = 2 then 1 end) as present_count ,count(distinct time) as Tot_count from Attendance join employee on duty.employee_id = employee.empid join approved_loan on approved_loan.employee_id=employee.empid 其中时间在 '2014-01-01' 和 '2016 -04-13' group by employee_id 它只返回一行:(以上是关于加入两张表获取考勤和用户名mysql的主要内容,如果未能解决你的问题,请参考以下文章