MySQL-每天休息两次,每天增加两次营业的营业时间

时间:2019-03-16 17:36:49

标签: php mysql phpmyadmin

这家餐厅的营业时间为星期一上午10点至星期二03上午,餐厅又在星期二上午10点至星期三03营业。

例如:
表格:business_hour

id | weedDay | openDay | openTime | closeDay | closeTime
-----------------------------------------------------------
1  | Monday  | Monday  | 10:00:00 | Tuesday  | 03:00:00 
2  | Tuesday | Tuesday | 10:00:00 | Wednday  | 03:00:00

如何使显示的MySQL SELECT语句是否保持打开状态?

<?php 

require_once "pdo/pdo.php";

    try {
    $stmt = $pdo->prepare("
            SELECT 
             CASE WHEN
                exists (
                  SELECT 1 FROM business_hour WHERE
                  (openday = dayname(now()) AND opentime <= time(now()))
                  OR
                  (closeday = dayname(now()) AND closetime >= time(now()))               
                )
                THEN 'Open'
                ELSE 'Closed'
              END status
            ");
    $stmt->execute();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        echo $row['status'];
    }


    } catch( PDOException $e ) {
        echo $e.'Not selected'; // error message
    }    
?>

1 个答案:

答案 0 :(得分:0)

我猜你想知道餐厅现在是否开门

true

请参见demo
仅当表select case when exists ( select 1 from business_hour where (openday = dayname(now()) and opentime <= time(now())) or (closeday = dayname(now()) and closetime >= time(now())) ) then 'Open' else 'Closed' end status; 中有有效的日期名称时,此方法才有效,
所以像business_hour而不是Wednday这样的错字会使其失败。