douman6679 2019-03-30 08:24
浏览 40

MySQL查询以获得商店的开放和关闭business_hour

I do get open shops but when I want to print closed shops the query print open shop as closed. My Goal is to make the same query to print open and closed shops because now I have two different queries.

What is difficult for me is that restaurants could be open two times per day. For example Restuarant, open Monday at 10 am till Tuesday 03 am and Tuesday at 10 am till Wednesday 03 am.

I tried to make a CASE WHEN query but the problem was it could not get restaurant ID with printed status.

My business_hour table with two inserted restaurants NOTE the restaurantID is a foreign key of a restaurant table.

 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

The first below query print open restaurant perfectly. How I get closed restaurants?

query for checking open restaurants

        $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++;   

        }

I tried this query

query for checking closed restaurants

  $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++;   

        }

I expect the output show me just one time closed and open restaurants.

The current day is Monday and Time is 7 am

OUTPUT

  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
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用