该餐厅的营业时间为星期一的上午10点至星期二03:00,餐厅在星期二的上午10点至星期三03:00再次营业。
例如:
表格: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
}
?>
最佳答案
我想您想知道餐厅现在是否营业:
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;
请参见demo
仅当表
business_hour
中有有效的日期名称时,此方法才有效,因此,像Wednday
这样的错字而不是Wednesday
会使它失败。关于php - MySQL-每天休息两次,增加营业时间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55199635/