php - MySQL 缺勤报告脚本

标签 php mysql sql join

我目前正在尝试编写一个查询,该查询将返回所有尚未在我们的时间表系统中记录给定日期时间的用户。我们目前有 2 个表、时间表和用户。我正在尝试进行一个查询,该查询将返回一个用户列表,这些用户在时间表表中没有某个日期范围的条目。每天的时间表表中只有一条记录,这应该很简单,但我无法弄清楚如何处理我的生活。

任何帮助将不胜感激:)。

+-----------------------+------------------+------+-----+---------+----------------+
| Field                 | Type             | Null | Key | Default | Extra          |
+-----------------------+------------------+------+-----+---------+----------------+
| timesheetID           | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| timesheetForUser      | int(11) unsigned | NO   |     |         |                |
| timesheetForDate      | date             | NO   |     |         |                |
| timesheetForCheckIn   | int(11)          | YES  |     | NULL    |                |
| timesheetNotes        | text             | YES  |     | NULL    |                |
| timesheetIsFilled     | tinyint(1)       | NO   |     |         |                |
| timesheetNoFillReason | int(11) unsigned | NO   |     |         |                |
| timesheetCreatedOn    | datetime         | NO   |     |         |                |
| timesheetCreatedBy    | int(11) unsigned | NO   |     |         |                |
| timesheetUpdatedOn    | datetime         | YES  |     | NULL    |                |
| timesheetUpdatedBy    | int(11) unsigned | YES  |     | NULL    |                |
+-----------------------+------------------+------+-----+---------+----------------+
+--------------------------------+---------------+------+-----+---------+----------------+
| Field                          | Type          | Null | Key | Default | Extra          |
+--------------------------------+---------------+------+-----+---------+----------------+
| userID                         | int(11)       | NO   | PRI | NULL    | auto_increment |
| userAccount                    | int(11)       | YES  |     | NULL    |                |
| userOrganization               | int(11)       | YES  |     | NULL    |                |
| userIsEmployee                 | tinyint(4)    | YES  |     | 0       |                |
| userEmployeeSince              | date          | YES  |     | NULL    |                |
| userName                       | varchar(255)  | YES  |     | NULL    |                |
| userTitle                      | varchar(255)  | YES  |     | NULL    |                |
| userEmail                      | varchar(255)  | YES  |     | NULL    |                |
| userLogin                      | varchar(50)   | YES  |     | NULL    |                |
| userPassword                   | varchar(255)  | YES  |     | NULL    |                |
| userSendInvitation             | tinyint(4)    | YES  |     | NULL    |                |
| userAddress1                   | varchar(255)  | YES  |     | NULL    |                |
| userAddress2                   | varchar(255)  | YES  |     | NULL    |                |
| userCity                       | varchar(255)  | YES  |     | NULL    |                |
| userCountry                    | char(2)       | YES  |     | NULL    |                |
| userState                      | varchar(6)    | YES  |     | NULL    |                |
| userStateOther                 | varchar(255)  | YES  |     | NULL    |                |
| userZip                        | varchar(20)   | YES  |     | NULL    |                |
| userPhone                      | varchar(50)   | YES  |     | NULL    |                |
| user_easypaycode               | varchar(6)    | YES  |     | NULL    |                |
| userFax                        | varchar(50)   | YES  |     | NULL    |                |
| userCell                       | varchar(50)   | YES  |     | NULL    |                |
| userTimezone                   | int(11)       | YES  |     | NULL    |                |
| userNotes                      | text          | YES  |     | NULL    |                |
| userActive                     | tinyint(4)    | NO   |     | 0       |                |
| userDisplayPictureType         | tinyint(4)    | YES  |     | NULL    |                |
| userDisplayPicture             | varchar(255)  | YES  |     | NULL    |                |
| userThumbnailPicture           | varchar(255)  | YES  |     | NULL    |                |
| userCanWriteMessages           | tinyint(4)    | NO   |     | 0       |                |
| userCanWriteComments           | tinyint(4)    | NO   |     | 0       |                |
| userCanUploadFiles             | tinyint(4)    | NO   |     | 0       |                |
| userCanCreateEvents            | tinyint(4)    | NO   |     | 0       |                |
| userCanCreateTickets           | tinyint(4)    | NO   |     | 0       |                |
| userCanManageProjects          | tinyint(4)    | NO   |     | 0       |                |
| userCanManageUsers             | tinyint(4)    | NO   |     | 0       |                |
| userCanManageOrganizations     | tinyint(4)    | NO   |     | 0       |                |
| userCanManageUserGroups        | tinyint(4)    | NO   |     | 0       |                |
| userCanManageMessageCategories | tinyint(4)    | NO   |     | 0       |                |
| userCanManageSetupOptions      | tinyint(4)    | NO   |     | 0       |                |
| userCanManageAllUsersItems     | tinyint(4)    | NO   |     | 0       |                |
| userCanEnterTimesheets         | tinyint(4)    | NO   |     |         |                |
| userCanManageTimesheets        | tinyint(4)    | NO   |     |         |                |
| userCanUseTimeclock            | tinyint(4)    | YES  |     | NULL    |                |
| userCanOnlyUseTimeclock        | tinyint(4)    | YES  |     | NULL    |                |
| userLastLogin                  | datetime      | NO   |     |         |                |
| userPWResetText                | varchar(255)  | YES  |     | NULL    |                |
| userDeleted                    | tinyint(4)    | NO   |     | 0       |                |
| userDeletedBy                  | int(11)       | YES  |     | NULL    |                |
| userDeletedOn                  | datetime      | YES  |     | NULL    |                |
| userMinHoursPerDay             | decimal(10,1) | YES  |     | NULL    |                |
+--------------------------------+---------------+------+-----+---------+----------------+

此外,如果一天没有记录时间,则不会在时间表中创建记录。

最佳答案

第一个查询获取所有在@start 和@end 之间没有注册的用户:

SELECT users.userName
FROM users
LEFT JOIN timesheets
ON timesheets.timesheetForUser = users.userID
AND timesheets.timesheetForDate BETWEEN @start AND @end
WHERE timesheets.timesheetForUser IS NULL

此查询获取所有丢失任何日期的用户以及他们丢失的日期(正如您在对问题的评论中所要求的):

SELECT dates.timesheetForDate, users.userName
FROM (SELECT DISTINCT timesheetForDate FROM timesheets) AS dates
CROSS JOIN users
LEFT JOIN timesheets
    ON timesheets.timesheetForUser = users.userID
    AND dates.timesheetForDate = timesheets.timesheetForDate
WHERE timesheets.timesheetForUser IS NULL

测试台:

CREATE TABLE timesheets (timesheetForUser int, timesheetForDate datetime);
INSERT INTO timesheets (timesheetForUser, timesheetForDate) VALUES
(1, '2010-01-01'),
(2, '2010-01-01'),
(3, '2010-01-01'),
(1, '2010-01-02'),
(3, '2010-01-02'),
(2, '2010-01-03'),
(2, '2010-01-04'),
(3, '2010-01-04');

CREATE TABLE users (userId int, userName nvarchar(100));
INSERT INTO users (userId, userName) VALUES
(1, 'Foo'),
(2, 'Bar'),
(3, 'Baz');

使用测试台的第二个查询的输出:

'2010-01-02 00:00:00', 'Bar'
'2010-01-03 00:00:00', 'Foo'
'2010-01-03 00:00:00', 'Baz'
'2010-01-04 00:00:00', 'Foo'

如果你愿意,你也可以将第二个查询创建为一个 View 并像这样查询它:

SELECT * FROM ViewMissingRegistrations
WHERE timesheetForDate BETWEEN @start AND @end

关于php - MySQL 缺勤报告脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2111306/

相关文章:

php - 将复选框值存储在 MySQL 数据库中,然后检索它们是否已设置……最好的方法是什么?

javascript - Laravel 和 Firebase 实时数据库连接

javascript - 公共(public)文件夹中的 Laravel 图像

mysql - JPA:与 3 个表连接时出错

mysql - 如何根据不相关的关系过滤 Symfony 表单实体输出

sql - 如何排除对称记录

php - 无法在 PHP 5.2.17 上激活 Xdebug

mysql - MySQL过程中插入错误

sql - 在 SQL Server 中使用动态名称访问另一个数据库

mysql - SQL 匹配行