Laravel选择位置/计数查询

时间:2018-07-14 13:08:46

标签: mysql laravel select count

有两个Mysql表:


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


  1. 表seat_reserveds(id,screening_id,reservation_id,seat_id)。

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

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

那我想算一下保留的座位数


从seat_resrveds中选择count(id),其中screening_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”:数字,因此我不能在$ reservedtitckets查询中使用它,因为我只需要一个数字。 如何编写这些查询。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

将查询更新为以下内容。

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

然后$reservation_id应该如下。

$reservation_id = $reservation->id;

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