【问题标题】:Laravel select where/count queryLaravel 选择位置/计数查询
【发布时间】:2018-07-14 13:08:45
【问题描述】:

Mysql有两个表:


1.reservations(id,screening_id,reserved)。 reserved 是一个布尔值,其值为“1”。


  1. 表座位预留 (id,screening_id,reservation_id,seat_id)。

我想要以下内容: 1.获取预订ID 从预订中选择 id 其中screening_id = id 和reserved = 1 在 laravel 中看起来像:

          $reservation_id = DB::table('reservations')->select('id')- 
          >where('screening_id',$id)->where('reserved','1')->get();

那我要数一下有多少位子预留


从 seat_resrveds 中选择 count(id),其中 screenshot_id = id 和 reservtain_id = id。 在 laravel 中看起来像:

        $reservedtickets = DB::table('seat_reseverds')- 
       >where('screening_id',$id)->where('reservation_id',$reservation_id)->count('id');

我认为问题在于我从 $reservation_id 获得了 "id":number,因此无法在 $reservedtitckets 查询中使用它,因为我只需要一个数字。 我该如何编写这些查询。

【问题讨论】:

    标签: mysql laravel select count


    【解决方案1】:

    使用->value('id') 从查询中获取单个值:

    $reservation_id = DB::table('reservations')
        ->where('screening_id',$id)
        ->where('reserved','1')
        ->value('id');
    

    【讨论】:

    • @Blasanka - 它甚至不是“雄辩的” - 它只是查询构建器。
    【解决方案2】:

    将查询更新为以下内容。

        $reservation = DB::table('reservations')
          ->where('screening_id',$id)
          ->where('reserved', 1)
          ->first();
    

    那么$reservation_id应该如下。

    $reservation_id = $reservation->id;
    

    您可以将此参数传递给您对$reservedtickets 的下一个查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-29
      • 2019-01-06
      • 1970-01-01
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      相关资源
      最近更新 更多