【问题标题】:Check if start time and end time partially overlapping using MYSQL php yii2?使用MYSQL php yii2检查开始时间和结束时间是否部分重叠?
【发布时间】:2021-04-25 11:59:57
【问题描述】:

伙计们 我一直在试图弄清楚如何检查两次之间的部分重叠。 基本上,我的时间保存在这样的数据库中:-

Batch::find()->alias('batch')->joinWith([
            'batchTeachers as teacher',
            'attendanceDates as attendanceDates',
            'batchTiming as batchTiming'
        ])->where([
            'OR', [
                'IN',
                'teacher.teacher_id',
                $teacher_id
            ],
            [
                'IN',
                'batch.teacher_id',
                $teacher_id
            ]
        ])->andWhere([
            'attendanceDates.date' => $date
        ])->andWhere([
            'OR',
            [
                'AND',
                [
                    '>=',
                    'batchTiming.start_time',
                    $calenderStartTime
                ],
                [
                    '<=',
                    'batchTiming.end_time',
                    $calenderEndTime
                ]

            ], [
                'AND', [
                    '<=',
                    'attendanceDates.start_time',
                    $calenderEndTime
                ], [
                    '>=',
                    'attendanceDates.end_time',
                    $calenderEndTime
                ]
            ]

        ])->exists()

上述查询的原始 SQL 为:

SELECT `batch`.* FROM `tbl_batch` `batch` LEFT JOIN `tbl_attendance_batch_teacher` `teacher` ON `batch`.`id` = `teacher`.`batch_id` LEFT JOIN `tbl_attendance_date` `attendanceDates` ON `batch`.`id` = `attendanceDates`.`batch_id` LEFT JOIN `tbl_batch_timing` `batchTiming` ON `batch`.`id` = `batchTiming`.`batch_id` WHERE ((`teacher`.`teacher_id`=1294) OR (`batch`.`teacher_id`=1294)) AND (`attendanceDates`.`date`='2021-01-06') AND (((`batchTiming`.`start_time` >= '08:00:00') AND (`batchTiming`.`end_time` <= '09:00:00')) OR ((`attendanceDates`.`start_time` <= '09:00:00') AND (`attendanceDates`.`end_time` >= '09:00:00'))) ORDER BY `date`

我的问题是如果用户在上午 9 点到 11 点之间有批次

如果他从上午 9 点到 11 点忙,那么 重叠(红色)

如果他从上午 9 点到 10 点忙,则 部分 重叠(蓝色)

【问题讨论】:

    标签: php mysql datetime yii2


    【解决方案1】:

    您需要在 SQL SELECT 中使用 CASE WHEN 语句

    SELECT
        ` batch `.*,
            CASE
                    WHEN ( ` batchTiming `.` start_time ` >= '08:00:00' AND ` batchTiming `.` end_time ` <= '09:00:00' ) THEN 1 -- partial overlap
                    WHEN ( ` attendanceDates `.` start_time ` <= '09:00:00' AND ` attendanceDates `.` end_time ` >= '09:00:00' ) THEN 2 -- full overlap
                    ELSE 0 -- no overlap
            END as overlap_value
    
    FROM
    

    参考here

    进一步将其转换为 Active Query 添加 select 子句,如下所示...

    Batch::find()
        ->alias('batch')
        ->select([ 'T.*', 'othrTable.*', ' CASE STATEMENT HERE as overlap_value'])
        ->joinWith( [...
    

    并在批处理模型中添加 $overlap_value 作为属性。

    【讨论】:

    • 感谢分享此内容,但有什么方法可以将其转换为 ActiveQuery 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    相关资源
    最近更新 更多