1

我之前问过一个类似的问题,但我已经缩小了范围并想开始一个新问题。

以下脚本将 To 和 From 日期插入 DB 记录。我想添加每隔一周跳过一次的选项。以下非常接近,但是它跳过了我不希望它做的第一周。有没有一种方法可以使用 COUNT 功能,并且只使用 Count 中的奇数记录并插入这些记录?

$week_day = date('w', $curr); # 0 - 6 to access the $week array

if ($week[$week_day]) { # skip if nothings in this day

    $date = date('Y-m-d', $curr);
    $sql->query("SELECT COUNT(schedule_id) FROM $pageDB WHERE doctor_id = $doc_id AND schedule_date = '$date'");

    if (!$sql->result(0)) { # skip if this is already defined
        $date = date('Y-m-d', strtotime("+1 week", $curr));
        $sql->query("INSERT INTO $pageDB (schedule_date, time, doctor_id, location_id) VALUES ('$date', '".$week[$week_day]."', $doc_id, '".$location[$week_day]."')");

    }
}
4

2 回答 2

0

您可以添加一个行号,然后使用 MOD (=> pos % 2) 来获取奇数行或偶数行。

像这样的东西:

SELECT 
    a.* 
FROM 
    (
        SELECT  
            @row := @row + 1 AS pos, 
            t.* 
        FROM 
            your-TABLE t, 
            (SELECT @row := 0) b
    ) a 
WHERE 
    pos % 2 = 0
于 2013-08-15T23:00:56.393 回答
0

您可以使用计数器并检查它是否是偶数/奇数modulus- %-if($j%2 != 0)

$week_day = date('w', $curr); # 0 - 6 to access the $week array

$j = 1; // counter

if ($week[$week_day]) { # skip if nothings in this day

    $date = date('Y-m-d', $curr);
    $sql->query("SELECT COUNT(schedule_id) FROM $pageDB WHERE doctor_id = $doc_id AND schedule_date = '$date'");

    if ($j%2 != 0) {
        $date = date('Y-m-d', strtotime("+1 week", $curr));
        $sql->query("INSERT INTO $pageDB (schedule_date, time, doctor_id, location_id) VALUES ('$date', '".$week[$week_day]."', $doc_id, '".$location[$week_day]."')");
         $j++; // increase the counter
    }
}
于 2013-08-15T23:06:29.283 回答