最近在做一个考勤系统,考勤主要关注的是缺勤、迟到和早退,目前的打卡控制器可以记录用户名和打卡时间,用户可能一天打卡多次,也可能一天只打了一次卡,这些情况都需要考虑。打卡信息都存储在考勤表中,从中要挖掘出一个月内的缺勤人员,迟到人员和早退人
最近在做一个考勤系统,考勤主要关注的是缺勤、迟到和早退,目前的打卡控制器可以记录用户名和打卡时间,用户可能一天打卡多次,也可能一天只打了一次卡,这些情况都需要考虑。打卡信息都存储在考勤表中,从中要挖掘出一个月内的缺勤人员,迟到人员和早退人员,并且能显示缺勤、迟到和早退的时间。
考勤表
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定期存储每年的节日,如果员工请假,也需要纳入到系统的考虑中。

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

To monitor the health and performance of MySQL servers, you should pay attention to system health, performance metrics and query execution. 1) Monitor system health: Use top, htop or SHOWGLOBALSTATUS commands to view CPU, memory, disk I/O and network activities. 2) Track performance indicators: monitor key indicators such as query number per second, average query time and cache hit rate. 3) Ensure query execution optimization: Enable slow query logs, record and optimize queries whose execution time exceeds the set threshold.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
