首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel雄辩查询在雄辩上失败,但在PhpMyAdmin中成功

Laravel雄辩查询在雄辩上失败,但在PhpMyAdmin中成功
EN

Stack Overflow用户
提问于 2015-10-29 14:14:39
回答 1查看 91关注 0票数 0

Laravel 4.2

这是我的桌子

sk__queue

代码语言:javascript
复制
+----------+----------+---------------------+--------+
| idQueue  | idCap    | date                | status |
+----------+----------+---------------------+--------+
| 1350     | 7        | 2015-10-20 11:20:00 | 9      |
| 1427     | 7        | 2015-10-29 14:20:00 | 4      |
| 1428     | 7        | 2015-10-29 14:19:00 | 4      |
+----------+----------+---------------------+--------+

sk__cloc

代码语言:javascript
复制
+----------+----------+-----------+-----+---------------------+--------------------------+
| idCloc   | idCap    | cDuration | cNb | begin               | end                      |
+----------+----------+-----------+-----+---------------------+--------------------------+
| 1        | 7        | 10        | 3   | 2015-01-01 08:00:00 | 2015-12-31 22:30:00      |
+----------+----------+-----------+-- --+---------------------+--------------------------+

如果sk__queue.status< 4> 7,则可用位置。

我想知道在过去的10分钟到10分钟的时间里有多少个位置可用。

我雄辩的质疑是这样的:

代码语言:javascript
复制
    //$now = date('Y-m-d H:i:s');
    $now = "2015-10-29 14:12:08";
    $dispo = DB::table('sk__cloc')->leftJoin('sk__queue', function($join) use ($now) {
        $join->on('sk__queue.idCap', '=', 'sk__cloc.idCap');
        $join->where('sk__queue.date', '>=', "('$now' - INTERVAL cDuration MINUTE)");
        $join->where('sk__queue.date', '<=', "('$now' + INTERVAL cDuration MINUTE)");
        $join->where('sk__queue.date', '>=', 'sk__cloc.begin');
        $join->where('sk__queue.date', '<=', 'sk__cloc.end');
        $join->where('sk__queue.status', '>=', 4);
        $join->where('sk__queue.status', '<=', 7);
    })
    ->where('sk__cloc.idCap', '=', 7)
    ->groupBy('cNb')
    ->select(DB::raw('cNb*2, count(sk__queue.idCap) AS count'))
    ->first();

结果:"cNb*2":6, "count":0

现在我正在做DB::getQueryLog(),这是laravel的请求:

代码语言:javascript
复制
    select cNb*2, count(sk__queue.idCap) AS count, sk__queue.date
    from `sk__cloc` left join `sk__queue`
    on `sk__queue`.`idCap` = `sk__cloc`.`idCap`
    and `sk__queue`.`date` >= ('2015-10-29 14:12:08' - INTERVAL cDuration MINUTE)
    and `sk__queue`.`date` <= ('2015-10-29 14:12:08' + INTERVAL cDuration MINUTE)
    and `sk__queue`.`date` <= sk__cloc.end
    and `sk__queue`.`status` >= 4
    and `sk__queue`.`status` <= 7
    where `sk__cloc`.`idCap` = 7
    group by cNb

在PhpMyAdmin中,结果是:"cNb*2":6, "count":2

然后我评论了这些台词:

代码语言:javascript
复制
    //$now = date('Y-m-d H:i:s');
    $now = "2015-10-29 14:12:08";
    $dispo = DB::table('sk__cloc')->leftJoin('sk__queue', function($join) use ($now) {
        $join->on('sk__queue.idCap', '=', 'sk__cloc.idCap');
        $join->where('sk__queue.date', '>=', "('$now' - INTERVAL cDuration MINUTE)");
        //$join->where('sk__queue.date', '<=', "('$now' + INTERVAL cDuration MINUTE)");
        //$join->where('sk__queue.date', '>=', 'sk__cloc.begin');
        $join->where('sk__queue.date', '<=', 'sk__cloc.end');
        $join->where('sk__queue.status', '>=', 4);
        $join->where('sk__queue.status', '<=', 7);
    })
    ->where('sk__cloc.idCap', '=', 7)
    ->groupBy('cNb')
    ->select(DB::raw('cNb*2, count(sk__queue.idCap) AS count'))
    ->first();

结果:"cNb*2":6, "count":2

我唯一的问题是:这里发生了什么事?!我真的很困惑。

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-29 16:34:12

你应该继续加入"on":

代码语言:javascript
复制
    $join->on('sk__queue.idCap', '=', 'sk__cloc.idCap');
    $join->on('sk__queue.date', '>=', "('$now' - INTERVAL cDuration MINUTE)");
    $join->on('sk__queue.date', '<=', "('$now' + INTERVAL cDuration MINUTE)");
    $join->on('sk__queue.date', '>=', 'sk__cloc.begin');
    $join->on('sk__queue.date', '<=', 'sk__cloc.end');
    $join->on('sk__queue.status', '>=', 4);
    $join->on('sk__queue.status', '<=', 7);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33416657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档