【问题标题】:Lumen/Laravel query builder - BETWEENLumen/Laravel 查询构建器 - BETWEEN
【发布时间】:2016-04-07 08:19:30
【问题描述】:

我正在尝试将这个传统的 sql 查询转换为 larvael/lumen 查询生成器并遇到砖墙。

$query = "SELECT a.id, a.name, a.cuisine, a.status, a.new, a.addressLine1, a.addressLine2, a.addressLine3, a.city, a.weekendot, a.weekendct, b.class
    FROM restaurants a, restaurants_class b
    WHERE a.class = b.id
    AND '$date' = CURRENT_DATE
    AND '$ntime' BETWEEN a.weekendot AND a.weekendct
    ORDER BY id DESC";

$date$ntime 是变量。

这是我目前所拥有的;

$posts = DB::connection('web')
        ->table('restaurants')
        ->join('restaurants_class', 'restaurants.class', '=', 'restaurants_class.id')
        ->select('restaurants.*', 'restaurants_class.*')
        ->get();

第一个AND 语句没有从DB 得到任何东西

第二个AND 语句将varDB 中的两列进行比较

不确定如何实现whereBetween

【问题讨论】:

    标签: php mysql laravel laravel-5 lumen


    【解决方案1】:

    这完全未经测试,但我认为您最好的选择是使用 whereRaw 方法,因为您有不允许使用 whereBetween 方法的变量。

    我还假设 $date$ntime 变量是 PHP 变量而不是 MySQL 变量。

    $posts = DB::connection('web')
           ->table('restaurants')
           ->join('restaurants_class', 'restaurants.class', '=', 'restaurants_class.id')
           ->whereRaw('? = CURRENT_DATE', $date)
           ->whereRaw('? BETWEEN a.weekendot AND a.weekendct', $ntime)
           ->select('restaurants.*', 'restaurants_class.*')
           ->get();
    

    【讨论】:

      猜你喜欢
      • 2021-09-25
      • 2016-12-27
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      相关资源
      最近更新 更多