获取几小时之间的记录 例如,上午5点到下午6点之间的记录 Sql语句如下
SELECT idProperty
from property_work_hours
WHERE day = '1'
AND (CONCAT('5:00', ' ', 'AM') BETWEEN CONCAT(`working_hour_from`, ' ', `from_period`)
AND CONCAT(`working_hour_to`, ' ', `to_period`)
or CONCAT('6:00', ' ', 'PM') BETWEEN CONCAT(`working_hour_from`, ' ', `from_period`)
AND CONCAT(`working_hour_to`, ' ', `to_period`)) GROUP BY idProperty
表
答案 0 :(得分:1)
不确定为什么不使用适当的字段类型。你问过
“例如,上午5点到下午6点之间的记录”
然后在凌晨1点有条件!我想你想要的是,任何人在凌晨5点到6点之间工作,无论他们何时开始或完成工作。
FirstName :<input type = "text" id = "firstname" value = "" required> <br>
LastName :<input type = "text" id = "lastname" value = "" required> <br>
Age :<input type = "number"id="age" value = "" required><br><br>
<button id = "add" onclick = "add()">Add</button>
<button id = "update" onclick = "add()">Update</button>
<button id = "clear" onclick = "clear()">Clear</button> <br><br>
<table border = "1" >
<th>FirstName</th>
<th>LastName</th>
<th>Age</th>
<th>Edit</th>
<th>Delete</th>
<tbody id = "arraytable"></tbody>
答案 1 :(得分:0)
这将返回搜索范围与行中范围重叠的任何行,包括搜索范围在行中的范围之前和之后开始的位置。
SELECT
idProperty
FROM property_work_hours
WHERE day = 1 AND
(
5 between (`working_hour_from` + if(`from_period` = 'PM',12,0)) AND (`working_hour_to` + if(`to_period` = 'PM',12,0)) OR
1 between (`working_hour_from` + if(`from_period` = 'PM',12,0)) AND (`working_hour_to` + if(`to_period` = 'PM',12,0)) OR
(`working_hour_from` + if(`from_period` = 'PM',12,0)) BETWEEN 1 AND 5 OR
(`working_hour_to` + if(`to_period` = 'PM',12,0)) BETWEEN 1 AND 5
)
它将匹配以下行:
IOW,搜索范围与行中范围重叠的任何行。
它只能工作几个小时。如果您需要包含会议记录,也可以轻松完成。它不是在比较中使用AM / PM,而是转换为24小时。