簡體   English   中英

創建SQL查詢

[英]Creation of SQL query

我有一個查詢問題..基本上在我的酒店預訂網站上有“預訂”,預訂中有一個入住日期和一個退房日期

我需要一個SQL查詢

檢測是否有新的檢查和 退房日期任何檢查的范圍,並在表退房日期

我當前的查詢不起作用,因為它無法檢測到新輸入的簽入日期何時開始於數據庫簽入日期之前,以及新輸入的簽出日期何時於數據庫中的簽出日期之后。

謝謝你的幫助!

當前代碼(我不確定您是否要使用純sql或...

// Gets the Checkin and checkout dates from the session variable
        $FirstDate = $request->session()->get('checkin');
        $SecDate = $request->session()->get('checkout');

        // Tries to match the checkin and checkout dates with other reservations of the same Hotel room
        foreach ($Rooms as $Room) {
            $Id =  $Room->id;

            $RoomsBooked = Reservation::where('room_id','=', $Id)
            ->where('CheckIn','>=',$FirstDate)
            ->where('CheckOut','<=',$SecDate)

                ->orWhere(function($query) use ($FirstDate,$Id)
                    {
                      $query->where('room_id','=', $Id)
                            ->where('CheckIn','<=',$FirstDate)

                            ->where('CheckOut','>=',$FirstDate);
                          })

                            ->orWhere(function($query2) use ($FirstDate,$SecDate,$Id)
                            {
                              $query2->where('room_id','=', $Id)
                                    ->where('CheckIn','>=',$FirstDate)

                                    ->where('CheckIn','<=',$SecDate);
                                  })->count();


                  // Works out How many rooms are available
                  $Roomsavailable = $Room->TotalRooms;
                  $Roomsleft =  $Roomsavailable - $RoomsBooked;
                  $Room->spaceleft = $Roomsleft;
            }

檢查兩個日期范圍是否重疊的基本邏輯很簡單:

where start1 < end2 and end1 > start2

如果要將同一日期視為重疊,請用<=等替換<

所以,你的代碼中,我想你只需要一個檢查一個地方:

        ->where('CheckIn','<=',$SecondDate)
        ->where('CheckOut','>=',$FirstDate);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM