从关系数据库中挑选日期php

时间:2016-08-26 13:58:30

标签: php sqlite

我正在创建一个rollcall类型的页面。

在这个数据库中,我有一个看似复杂的东西(可能不是,我可能不会过分思考它)。

这是我的结构:

rollcall_teammates:
Lists all my users
-------------------------------------
| uniqueID | Name | LineID | Active |
-------------------------------------
|        1 |  Bob |      1 |      1 |
-------------------------------------
|        2 |  Sal |      2 |      1 |
-------------------------------------

rc_teammates_days:
The actual attendance of the users, the UserID is the uniqueID of my rollcall_teammates table.
--------------------------------------------------------------------------------------
| uniqueID | UserID | TimeIn | TimeOut |  Timestamp | hadLunch | Complete |   Status |
--------------------------------------------------------------------------------------
|        1 |      1 | 7:30AM |  4:00PM | 08/26/2016 |        1 |        0 | Attended |
--------------------------------------------------------------------------------------
|        2 |      2 | 7:30AM |  4:00PM | 08/26/2016 |        1 |        0 | Attended |
--------------------------------------------------------------------------------------

rc_teammates_jobs:
Jobs are not added for every single day, only when changed. The UserID is based off of rollcall_teammates aswell.
-----------------------------------------------
| uniqueID | UserID |        Job |  Timestamp |
-----------------------------------------------
|        1 |      1 | Technician | 08/26/2016 |
-----------------------------------------------
|        1 |      2 | Technician | 08/26/2016 |
-----------------------------------------------

我的问题是,对于任何输入的日子,我需要计算那天活跃的工作。

我成功地使用当前日期执行此操作,因为它始终是输入作业表的最后一个作业。

在查看过去的日期时,这显然是不正确的

所以我需要做的是在输入日期或之前获得最接近的记录日期(来自作业数据库)。

这背后的逻辑,只是想一想,让我感到困惑。所以一些帮助将非常感激。我现在的目的是:

这基本上会列出今天活动的工作列表(仅适用于当天)并计算它们。

$sql = "SELECT uniqueID,Name from rollcall_teammates WHERE $searchLines AND Active=1;";
$ret = $db->query($sql);

$jobsToday = array();
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){

    $attend = $db->querySingle("SELECT Status from rc_teammates_days WHERE UserID='".$row['uniqueID']."' AND Timestamp='".date("m/d/Y")."' ORDER BY uniqueID DESC LIMIT 1;");
    if((!empty($attend)) && (strcmp($attend,"Incomplete") != 0)) {
        $job = $db->querySingle("SELECT Job from rc_teammates_jobs WHERE UserID='".$row['uniqueID']."' ORDER BY uniqueID DESC LIMIT 1;");
        if((strcmp($attend,"Attended") == 0) || (strcmp($attend,"In Late (BL)") == 0) || (strcmp($attend,"In After Lunch") == 0)) {
            array_push($jobsToday,$job);
        }
    }

}

$distinct = array_unique($jobsToday);
$counts = array_count_values($jobsToday);

echo "<ul>";
foreach($distinct as &$value) {
    echo "<li>$value (".$counts[$value].")</li>";
}
echo "</ul>";

0 个答案:

没有答案