1

这是我的选择声明。它有什么作用?

  • 检查数据库中是否已经有给定日期的约会
  • 检查用户是否在该特定时间工作。

mysql_num_rows($result)我检查是否有任何行。如果返回 0,则可以添加约会,如果返回其他内容(数据库中已经有约会),则被阻止。

该语句适用于第 1 点。如果已经有约会,则返回 1 或更多。但是,当所选日期超出用户的工作时间范围时,它也返回0。

问题:当用户在特定时间不工作时,我可以在语句中添加什么以获得返回的行(mysql_num_rows返回 1 或更多) ?

工作时间范围示例:08:00:00 to 17:00:00

select * 
from ios_appointments a join ios_workinghours h using(workerid_fk) 
where workerid_fk = 1 
AND h.start <= '19:57:20' AND h.end >= '20:57:20' 
AND ( 
   a.start BETWEEN '2012-12-24 19:57:20'  AND  '2012-12-24 20:57:20' 
   OR  a.end   BETWEEN '2012-12-24 19:57:20'  AND  '2012-12-24 20:57:20' 
   OR (a.start < '2012-12-24 19:57:20' AND a.end > '2012-12-24 20:57:20') 
)
4

1 回答 1

0

您可以返回一行空值:

select * 
from ios_appointments a join ios_workinghours h using(workerid_fk) 
where workerid_fk = 1 
AND h.start <= '19:57:20' AND h.end >= '20:57:20' 
AND ( 
   a.start BETWEEN '2012-12-24 19:57:20'  AND  '2012-12-24 20:57:20' 
   OR  a.end   BETWEEN '2012-12-24 19:57:20'  AND  '2012-12-24 20:57:20' 
   OR (a.start < '2012-12-24 19:57:20' AND a.end > '2012-12-24 20:57:20') 
)
union
select null, null, null -- how every many column are in the first part
where not exists (
    select * 
    from ios_appointments a join ios_workinghours h using(workerid_fk) 
    where workerid_fk = 1 
    AND h.start <= '19:57:20' AND h.end >= '20:57:20' 
    AND ( 
       a.start BETWEEN '2012-12-24 19:57:20'  AND  '2012-12-24 20:57:20' 
       OR  a.end   BETWEEN '2012-12-24 19:57:20'  AND  '2012-12-24 20:57:20' 
       OR (a.start < '2012-12-24 19:57:20' AND a.end > '2012-12-24 20:57:20') 
    )
)

如果您的查询一无所获,这将返回一行空值,或者如果找到行则表现得像现在一样。

您可以返回任何您想要的值,而不是 null。

于 2012-12-26T19:26:24.337 回答