Mysql:查询无法正常工作,没有选择12:00之间

时间:2019-08-27 14:06:01

标签: php mysql laravel

mysql between无法正常工作。 查询我现在有:

Table::where(function ($query) use ($time_in, $time_out) {
            $query->orWhereRaw("? between time_in AND time_out", [$time_in.":00"])
            ->orWhereRaw("? between time_in AND time_out", [$time_out.":00"])
            ->orWhereRaw("time_in between ? AND ?", [$time_in.":00", $time_out.":00"])
            ->orWhereRaw("time_out between ? AND ?", [$time_in.":00", $time_out.":00"]); 
        })->get();

如果我的Tabletime_intime_out和{{1}之间,我想从time_in获取所有数据}。 在这里不起作用的是: 如果time_out$time_in = 12:00,那么我的查询将不会选择$time_out = 3:00time_in的{​​{1}}的数据。但是,如果我将11:00更改为time_out = 4,则查询有效。如果我有$time_in1:00的数据,并且如果我检查自己的11:0012:00是否在它们之间,则无法正常工作

2 个答案:

答案 0 :(得分:1)

我假设您使用$ time_in和$ time_out的12小时制,因为到12:00时您似乎是指午夜。

不满足BETWEEN条件,因为“ 12:00”大于“ 03:00”,在这种情况下MySQL将永远不会返回任何数据。

在表示12:00 am时,您需要先将“ 12”转换为“ 00”。

或者您应该找到另一种方式让MySQL知道您的意思是12:00 am而不是12:00 pm。

此外,您如何区分例如凌晨4点和下午4点?

我很高兴我总是使用24小时制,其中12:00 am只是00:00。

答案 1 :(得分:0)

如果要检查重叠的时间段,请尝试以下操作:

#include <stdio.h>

#define NOT_PRESENT -1
#define is_present(x) ((x) != NOT_PRESENT)

struct cube {  
    int height;
    int length;
    int width;
};

int calculate(struct cube);

int main() {
    struct cube shape = {
        .height = NOT_PRESENT,
        .length = NOT_PRESENT,
        .width = NOT_PRESENT,
    };

    shape.height = 2;
    shape.width = 3;

    printf("Area: %d\n", calculate(shape)); // Prints 6

    shape.length = 4;
    printf("Volume: %d\n", calculate(shape)); // Prints 24

    return 0;
}

int calculate(struct cube nums) {
    if (is_present(nums.height) && is_present(nums.width) && is_present(nums.length)) {
        return nums.height * nums.width * nums.length;
    } else if (is_present(nums.height) && is_present(nums.width)) {
        return nums.height * nums.width;
    } else {
        return -1; // Error
    }
}

在上面的示例中,我的句点在$time_in = $time_in.':00'; $time_out = $time_out.':00'; $query->where(function ($mainQuery) use ($time_in, $time_out) { $mainQuery->where(function ($firstQuery) use ($time_in, $time_out) { // starts within period $firstQuery->where('time_in', '>=', $time_in) ->where('time_in', '<', $time_out); })->orWhere(function ($secondQuery) use ($time_in, $time_out) { // ends within period $secondQuery->where('time_out', '>', $time_in) ->where('time_out', '<=', $time_out); }) ->orWhere(function ($thirdQuery) use ($time_in, $time_out) { // starts before period, ends after it $thirdQuery->where('time_in', '<', $time_in) ->where('time_out', '>', $time_out); }); }); 上排他,这表示

time_out

这样,您可以在两个句号之间依次设置两个time_in <= in_period < time_out