最近在做一个考勤系统,考勤主要关注的是缺勤、迟到和早退,目前的打卡控制器可以记录用户名和打卡时间,用户可能一天打卡多次,也可能一天只打了一次卡,这些情况都需要考虑。打卡信息都存储在考勤表中,从中要挖掘出一个月内的缺勤人员,迟到人员和早退人
最近在做一个考勤系统,考勤主要关注的是缺勤、迟到和早退,目前的打卡控制器可以记录用户名和打卡时间,用户可能一天打卡多次,也可能一天只打了一次卡,这些情况都需要考虑。打卡信息都存储在考勤表中,从中要挖掘出一个月内的缺勤人员,迟到人员和早退人员,并且能显示缺勤、迟到和早退的时间。
考勤表
CREATE TABLE [dbo].[kaoqin]( [user_name] [varchar](50) NULL, [card_time] [datetime] NULL ) ON [PRIMARY] GO插入测试数据
INSERT INTO [master].[dbo].[kaoqin] select '张三', '2014-08-03 09:36:00' union all select '张三', '2014-08-03 18:10:00' union all select '张三', '2014-08-04 08:32:00' union all select '张三', '2014-08-04 15:15:00' union all select '张三', '2014-08-05 09:32:00' union all select '张三', '2014-08-05 15:15:00' union all select '张三', '2014-08-01 08:36:00' union all select '张三', '2014-08-01 18:10:00' union all select '张三', '2014-08-02 08:32:00' union all select '张三', '2014-08-02 18:15:00' union all select '张三', '2014-08-25 08:00:00' union all select '张三', '2014-08-24 19:00:00' union all select '张三', '2014-08-27 08:00:00' union all select '张三', '2014-08-27 17:00:00' union all select '张三', '2014-08-26 10:00:00' union all select '张三', '2014-08-26 18:30:00' union all select '张三', '2014-08-26 8:00:00' union all select '张三', '2014-08-27 18:56:00' GO我的思路是用一张临时表得到这个月的所有工作日,将该临时表与用户进行交叉连接,这样每个用户在这个月的每个工作日都有一条记录。假设早上9点为上班时间,18点为下班时间,这个可以后续做成变量的形式。
declare @time_start datetime declare @time_end datetime set @time_start = '2014-08-01 00:00:00' set @time_end = DATEADD(M,1,@time_start) -- 一个月的工作日 IF object_id('tempdb..#tempDate') is not null BEGIN drop table #tempDate END CREATE table #tempDate ( stat_day varchar(10) ) IF object_id('tempdb..#tempUserDate') is not null BEGIN drop table #tempUserDate END CREATE table #tempUserDate ( stat_day varchar(10), [user_name] varchar(40) ) CREATE clustered index tempUserDate_Index1 on #tempUserDate ([user_name],stat_day) declare @time_temp datetime set @time_temp = @time_start while @time_temp < @time_end begin if datepart(weekday,@time_temp)>1 and datepart(weekday,@time_temp)<7 begin insert into #tempDate (stat_day) values (CONVERT(varchar(10),@time_temp,121)) end set @time_temp= dateadd(d,1,@time_temp) end insert into #tempUserDate select * from #tempDate cross join (select distinct [user_name] from [kaoqin]) t从原始的kaoqin表中查询出每个用户的上班时间和下班时间,如果用户一天的打开记录超过两条,那么就会取最早和最晚的一条分别作为上班时间和下班时间。
select [user_name],CONVERT(varchar(10),card_time,121) as stat_day, MIN(card_time) as on_time,MAX(card_time) as off_time from [kaoqin] group by [user_name],CONVERT(varchar(10),card_time,121)通过临时表#tempUserDate和上面的查询结果关联,如果左联接为空,则证明该人员缺勤。
--缺勤 select * from #tempUserDate a left join ( select [user_name],CONVERT(varchar(10),card_time,121) as stat_day, MIN(card_time) as on_time,MAX(card_time) as off_time from [kaoqin] group by [user_name],CONVERT(varchar(10),card_time,121) ) b on a.[user_name]=b.[user_name] and a.stat_day=b.stat_day where [b].[user_name] is null下面是迟到和早退的实现SQL。
--迟到 select * from #tempUserDate a left join ( select [user_name],CONVERT(varchar(10),card_time,121) as stat_day, MIN(card_time) as on_time,MAX(card_time) as off_time from [kaoqin] group by [user_name],CONVERT(varchar(10),card_time,121) ) b on a.[user_name]=b.[user_name] and a.stat_day=b.stat_day where CONVERT(varchar(100), [b].[on_time], 8)>'09:00:00' --早退 select * from #tempUserDate a left join ( select [user_name],CONVERT(varchar(10),card_time,121) as stat_day, MIN(card_time) as on_time,MAX(card_time) as off_time from [kaoqin] group by [user_name],CONVERT(varchar(10),card_time,121) ) b on a.[user_name]=b.[user_name] and a.stat_day=b.stat_day where CONVERT(varchar(100), [b].[off_time], 8)<'18:00:00'得到的结果
如果某个人他今天既迟到又早退在最终的结果中都会体现,可以从2014-08-05这条数据看出。当然,这个考勤系统还不完善,例如没有将节日考虑进来,初步的考虑是采用Job定期存储每年的节日,如果员工请假,也需要纳入到系统的考虑中。

mySqlStringTypesimpactStorageAndPerformanCeaseAsfollows:1)长度,始终使用theSamestoragespace,whatcanbefasterbutlessspace-felfficity.2)varCharisvariable varcharisvariable length,morespace-morespace-morespace-effficitybuteftife buteftife butfority butfority textifforlyslower.3)

mySqlStringTypesIncludeVarChar,文本,char,enum和set.1)varCharisVersAtileForvariable-lengthStringStringSuptOptoPeptoPepecifientlimit.2)textisidealforlargetStortStorStoverStorextorewiteWithoutAdefinedLengthl.3)charlisfixed-Length

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

toadduserInmysqleffectection andsecrely,theTheSepsps:1)USEtheCreateuserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNectalRevileSaryPrivilegesSustate,usiveleanttatement,AdheringTotheTeprinciplelastPrevilegege.3)

toaddanewuserwithcomplexpermissionsinmysql,loldtheSesteps:1)创建eTheEserWithCreateuser'newuser'newuser'@''localhost'Indedify'pa ssword';。2)GrantreadAccesstoalltablesin'mydatabase'withGrantSelectOnMyDatabase.to'newuser'@'localhost';。3)GrantWriteAccessto'

MySQL中的字符串数据类型包括CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT,排序规则(Collations)决定了字符串的比较和排序方式。1.CHAR适合固定长度字符串,VARCHAR适合可变长度字符串。2.BINARY和VARBINARY用于二进制数据,BLOB和TEXT用于大对象数据。3.排序规则如utf8mb4_unicode_ci忽略大小写,适合用户名;utf8mb4_bin区分大小写,适合需要精确比较的字段。

最佳的MySQLVARCHAR列长度选择应基于数据分析、考虑未来增长、评估性能影响及字符集需求。1)分析数据以确定典型长度;2)预留未来扩展空间;3)注意大长度对性能的影响;4)考虑字符集对存储的影响。通过这些步骤,可以优化数据库的效率和扩展性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。