mysql处理考勤数据_MySql中的考勤报告

bd96500e110b49cbb3cd949968f18be7.png

I want to write a query to generate attendance report of employee. First I will tell you how the presence of employee is stored in my database.

I have following tables.

Employee Table with Columns

emp_id emp_Name Joining_Date

1 john 11-01-2012

2 Scott 12-01-2012

Holiday Table

Holiday_Name Date

Chrismas 25-12-2012

Dushera 08-03-2012

Independance Day 15-08-2012

Leave Table

Subject from_Date to_Date Emp_Id status

PL 02-01-2012 04-01-2012 1 Approved

CL 11-01-2012 12-01-2012 2 Declined

Doctor Table

Subject Call_Date call_Done_By(emp_id)

Call 15-01-2012 1

CA 21-02-2012 2

Chemist Table

Subject Call_Date call_Done_By(emp_id)

Chemist 1-02-2012 2

Texo 21-03-2012 1

If employee is visited to doctor or chemist,that particular date is stored in that particular doctor or chemist table with employee_id

Now person will select year and month and he should be able to get attendance report in following format

Example : suppose user selects year as '2011' and month as 'Dec' then output should be

Employee year Month 1 2 3 4 5 6 7....

John 2011 Nov Y Y Y Y Y L S....

Scott 2011 Nov Y Y L M Y L S

here in output 1,2,3.... are days from 0-30 for a month which we can write using 'case'

Consider if employee is present on day show its status as 'Y' else L else

if he gone to any customer like doctor,chemist,then replace it with 'S'.

So how should I write a query to achieve this output??

any suggestions will be helpful for me....

解决方案

Here is a long way that should work as expected:

SELECT

Employee.emp_Name,

'2011' AS `Year`,

'Dec' AS `Month`,

CASE (

IF(

DATE('1-12-2011') < DATE(Employee.Joining_Date)),

'0' --Not joined yet

IF (

(SELECT COUNT(*) FROM Holiday WHERE DATE('1-12-2011') = DATE(Holiday.date)) = 1,

'1', --National Holiday

IF (

(SELECT COUNT(*) FROM Leave WHERE DATE('1-12-2011') > DATE(Leave.to_Date) AND DATE('1-12-2011') < DATE(Leave.from_Date) AND Leave.Emp_Id = Employee.emp_id) = 1,

'2', --On Leave

IF(

(SELECT COUNT(*) FROM Doctor WHERE DATE('1-12-2011') > DATE(Doctor.Call_Date) AND Doctor.call_Done_By = Employee.emp_id) = 1 OR

(SELECT COUNT(*) FROM Chemist WHERE DATE('1-12-2011') > DATE(Chemist.Call_Date) AND Chemist.call_Done_By = Employee.emp_id) = 1,

'3' --Visit Doctor or Chemist

'4' --Employee was at work

)

)

)

)

)

WHEN 0 THEN 'N/A' --Not joined yet

WHEN 1 THEN 'L' --National Holiday

WHEN 2 THEN 'L' --On Leave

WHEN 3 THEN 'S' --Visit Doctor or Chemist

ELSE 'Y' --Employee was at work

END AS `1`, --first day of month

... AS `2`, --repeat for second day of the month till max day of current month replace '1-12-2011' with each different day of month

...

... AS `30`

FROM

Employee

My suggestion is to create a view that does the if statement for each employee that way your code will be easier to maintain. Please keep in mind that this is pseudo code that might need some some changing to run.

Hope this helps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于mysql考勤系统设计函数,我可以给你一些建议。不过在此之前,我们需要先建立一个员工考勤系统的数据库,以便后续的开发。 下面是一个简单的员工考勤系统数据库结构设计,包括员工信息表、考勤记录表和请假记录表: 1.员工信息表(employee_info): | 字段名 | 类型 | 描述 | | ---------- | ------------- | -------------- | | id | int(11) | 员工ID | | name | varchar(50) | 姓名 | | sex | varchar(2) | 性别 | | birthday | datetime | 出生日期 | | department | varchar(100) | 所属部门 | | position | varchar(100) | 职位 | | phone | varchar(20) | 联系电话 | | email | varchar(100) | 电子邮箱 | | hire_date | datetime | 入职日期 | | status | int(11) | 员工状态(1:在职,0:离职) | 2.考勤记录表(attendance_record): | 字段名 | 类型 | 描述 | | --------- | ---------- | ------------ | | id | int(11) | 记录ID | | employee | int(11) | 员工ID | | date | datetime | 日期 | | sign_in | datetime | 上班打卡时间 | | sign_out | datetime | 下班打卡时间 | | work_time | varchar(8) | 工作时长 | 3.请假记录表(leave_record): | 字段名 | 类型 | 描述 | | --------- | ---------- | ---------- | | id | int(11) | 记录ID | | employee | int(11) | 员工ID | | start | datetime | 请假开始时间 | | end | datetime | 请假结束时间 | | leave_day | int(11) | 请假天数 | | reason | varchar(50) | 请假原因 | 关于mysql考勤系统设计函数,我们可以设计一些用于计算员工工作时长、请假天数等的存储过程或触发器。例如,在考勤记录表添加一条记录时,可以自动计算出员工的工作时长,并更新到员工信息表。在请假记录表添加一条记录时,可以自动计算出请假天数,并更新到员工信息表。 当然,具体函数的设计还需要根据实际需求进行调整和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值