简体   繁体   English

MySQL和PHP,比较两个表,获取日期,并分配值

[英]MySQL and PHP, Comparing two tables, getting the date, and assigning values

I currently have 2 tables. 我目前有2张桌子。 One with employee clockin times and one with their schedules. 一个与员工的上班时间相关,另一个与他们的时间表相关。 What I am trying to accomplish is to run this once a day to check to see who was ontime, early, late or absent. 我要完成的工作是每天运行一次,检查谁在准时,早,晚或缺勤。 Thanks to fabianhjr on my last post I was able to find people who were early or on time using MySQL like so 多亏了fabianhjr在我的上一篇文章中,我才能够找到像MySQL这样早或按时使用的人

    $query = "SELECT cms.date, cms.userID, cms.login, oc_schedule.start_time, oc_schedule.userID 
    FROM cms 
    INNER JOIN oc_schedule ON oc_schedule.start_time >= cms.login AND oc_schedule.userID = cms.userID";

Now to find people who were late or absent I have both tables CMS and oc_schedule. 现在要找到迟到或缺席的人,我同时拥有CMS和oc_schedule表。 CMS is the table that tracks when people have logged in (its a pre-existing system) and oc_schedule which I have just made and input all the schedules in a table with rows userID, Sunday-Saturday with either Y or N as their values, start_time, and end_time. CMS是一个表,用于跟踪用户何时登录(我已经建立了该系统)和oc_schedule并将它们输入到表中,所有表都以userID,星期日至星期六的行(以Y或N作为值)输入,开始时间和结束时间。

So what I need to do now is to check CMS for the date, find the name of the day, assign it a value of Y, check in oc_schedule for everyone who has that day with the Value Y and then check for the schedule start_time and if they logged in and if so what time. 因此,我现在需要做的是检查CMS的日期,找到日期的名称,为其指定Y的值,为oc_schedule检查当天有值Y的每个人,然后检查计划的start_time和他们是否登录以及如果登录了什么时间。 Move the people who clocked in on time or early into another table, and every one who was late or absent into the late_absent table. 将按时或早点报到的人员转移到另一张桌子中,并将每一个迟到或缺席的人员都移到late_absent表中。

So far this is as far as I have gotten for the late and absent list. 到目前为止,这是我所获得的最新名单。 Right now all it does is find logins that are either null or greater then the schedule time but it doesnt really work without it comparing the dates. 现在,它所做的只是查找空值或大于计划时间的登录名,但是如果不比较日期,它实际上是行不通的。 And I am echoing the tables right now just to see that it works without having to post to the Database. 我现在正在回显这些表,只是为了查看它的工作原理而不必发布到数据库。

    <?php 
    mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("sacc") or die(mysql_error()); 

    $query = "SELECT cms.date, cms.userID, cms.login, oc_schedule.start_time, oc_schedule.userID, oc_schedule.sunday, oc_schedule.monday, oc_schedule.tuesday, oc_schedule.wednesday, oc_schedule.thursday, oc_schedule.friday, oc_schedule.saturday
    FROM cms
    RIGHT OUTER JOIN oc_schedule
    ON oc_schedule.userID = cms.userID AND oc_schedule.start_time < cms.login";

    $result = mysql_query($query) or die(mysql_error());

    echo "<table cellpadding='5' border='0' style='border: 1px solid black;border-collapse:collapse'>
    <tr>
    <th style='border: 1px solid black;'>Date</th>
    <th style='border: 1px solid black;'>CID/WIN</th>
    <th style='border: 1px solid black;'>Scheduled Login</th>
    <th style='border: 1px solid black;'>Late Login Time</th>
    </tr>";

    while($row = mysql_fetch_array($result)) {

    echo "<tr style='text-align: center;'>";
    echo "<td style='border: 1px solid black;'>" . $row['date'] . "</td>";
    echo "<td style='border: 1px solid black;'>" . $row['userID'] . "</td>";
    echo "<td style='border: 1px solid black;'>" . $row['start_time'] . "</td>";
    echo "<td style='border: 1px solid black;'>" . $row['login'] . "</td>";
    echo "</tr>";

    }
    echo "</table>";
    ?>

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

Without testing it, this could be an answer. 如果不进行测试,这可能是一个答案。 It gives a table with a header per type (early/ontime/late/absent), followed by all records that are that type. 它为每个类型的表提供了一个表头(早期/准时/延迟/缺席),后跟该类型的所有记录。

$query  = "SELECT ";
$query .=     "cms.date, cms.userid, cms.login, ";
$query .=     "s.start_time, s.sunday, s.monday, s.tuesday, s.wednesday, s.thursday, s.friday, s.saturday ";
$query .= "FROM ";
$query .=     "cms ";
$query .=     "RIGHT OUTER JOIN oc_schedule s ON s.userid = cms.userid ";
$query .= "ORDER BY ";
$query .=     "(s.start_time - cms.login) AS offset";

$result = mysql_query($query) or die(mysql_error());

$lasttype = "";

while($row = mysql_fetch_array($result)) {
    if($row['cms.login'] == ""){
        $curtype = 'Absent';
    } else {
        switch($row['offset']){
            case > 0: $curtype = 'Early'; break;
            case < 0: $curtype = 'Late'; break;
            case 0: $curtype = 'On Time'; break;
        }
    }

    if($curtype != $lasttype){
        ?>
            <tr>
                <th colspan="4"><?=$curtype?></th>
            </tr>
        <?
    }

    ?>
        <tr>
            <td><?=$row['date']?></td>
            <td><?=$row['userid']?></td>
            <td><?=$row['start_time']?></td>
            <td><?=$row['login']?></td>

        </tr>
    <?

    $lasttype = $curtype;
}

Example: 例:

ABSENT
20-jan-12 123 09:00:00 NULL
20-jan-12 124 09:00:00 NULL
LATE
20-jan-12 125 09:00:00 09:20:00
20-jan-12 126 09:00:00 09:05:00
ON TIME
20-jan-12 127 09:00:00 09:00:00
EARLY
20-jan-12 128 09:00:00 08:50:00
20-jan-12 129 09:00:00 08:30:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM