0

我确实有开放的商店,但是当我想打印已关闭的商店时,查询会将开放的商店打印为已关闭。我的目标是对打开和关闭的商店进行相同的查询,因为现在我有两个不同的查询。

对我来说困难的是餐厅每天可以开两次。例如餐厅,营业时间为周一上午 10 点至周二凌晨 3 点,周二上午 10 点至周三凌晨 3 点。

我尝试进行 CASE WHEN 查询,但问题是它无法获取带有打印状态的餐厅 ID。

我的带有两个插入餐厅的 business_hour 表注意 restaurantID 是餐厅表的外键。

 id,restaurantID,openday,opentime,closeday,closetime
 1,15,Monday,10:00:00,Tuesday,03:00:00
 2,15,Tuesday,10:00:00,Wednesday,03:00:00
 3,24,Monday,10:00:00,Tuesday,03:00:00
 4,24,Tuesday,10:00:00,Wednesday,03:00:00
 5,24,Wednesday,10:00:00,Thursday,04:00:00
 6,24,Thursday,10:00:00,Friday,02:00:00
 7,24,Friday,10:00:00,Saturday,01:00:00
 8,24,Saturday,11:00:00,Sunday,01:00:00
 9,24,Sunday,11:00:00,Monday,02:00:00

下面的第一个查询完美地打印开餐厅。我如何获得关闭的餐厅?

检查开放餐厅的查询

        $stmt = $pdo->prepare("SELECT * FROM                         
                    business_hour 
                        INNER JOIN restaurant
                           ON business_hour.restaurantID = 
                            restaurant.id   
                    WHERE         
                     (closeday = '$today' AND closetime >= 
                      '$timeNow')  
                       OR
                     (openday = '$today' AND opentime <= 
                             '$timeNow')
                      ");  

        $stmt->execute();
        $i=0;
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo $row['ravintolaID'].' '.'OPEN'.'  '.$row['openday'].' '.$row['opentime'].' --'.$row['closeday'].' '.$row['closetime'].' '.'<br>';

            $i++;   

        }

我试过这个查询

检查关闭餐厅的查询

  $stmt = $pdo->prepare("SELECT * FROM business_hour 
    INNER JOIN restaurant
   ON business_hour.restaurantID = restaurant.id    
    WHERE         
      (closeday = '$today' AND closetime < '$timeNow')  
           OR
       (openday = '$today' AND opentime > '$timeNow')
     ");    
    $stmt->execute();
        $i=0;
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo $row['ravintolaID'].' '.'CLOSED'.' open at '.$row['openday'].' '.$row['opentime'].' --  '.$row['closeday'].' '.$row['closetime'].' '.'<br>';

            $i++;   

        }

我希望输出只显示一次关闭和打开的餐厅。

当天是星期一,时间是早上 7 点

输出

  24 OPEN Sunday 10:00:00 --Monday 08:00:00 
  15 CLOSED open at Monday 10:00:00 -- Tuesday 03:00:00 
  15 CLOSED open at Sunday 10:00:00 -- Monday 03:00:00 
  24 CLOSED open at Monday 10:00:00 -- Tuesday 03:00:00
4

0 回答 0