0

我有一个应用程序,用户可以在其中定义他的工作时间:例如从早上 8 点到下午 5 点。在我的 MySQL 数据库中,我有两个时间戳字段。如何只设置时间而不设置日期?我需要这个,因为只有时间很重要。我只想知道用户从什么时候到什么时候工作。日期本身无关紧要。

我尝试过这样的事情,但我的数据库拒绝了它:

0000-00-00 08:00:00

这是我的选择语句:

# check if there is already an appointment, if yes, return it
select * 
from ios_appointments a join ios_workinghours h using(workerid_fk)
where workerid_fk = 1
AND h.start <= '08:30:00' AND h.end >= '10:00:00'
AND (
    a.start BETWEEN '2012-12-24 08:30:00'  AND  '2012-12-24 10:00:00'
    OR  a.end   BETWEEN '2012-12-24 08:30:00'  AND  '2012-12-24 10:00:00'
    OR (a.start < '2012-12-24 08:30:00' AND a.end > '2012-12-24 10:00:00')
)
4

4 回答 4

0

not sure if this is what your are asking for but u can use DATE_FORMAT

select * 
  from ios_appointments a join ios_workinghours h using(workerid_fk)
  where workerid_fk = 1
        AND DATE_FORMAT(h.start , '%H:%i:%s') <= '08:30:00' AND DATE_FORMAT(h.end , '%H:%i:%s') >= '10:00:00'
        AND (
          a.start BETWEEN '2012-12-24 08:30:00'  AND  '2012-12-24 10:00:00'
          OR  a.end   BETWEEN '2012-12-24 08:30:00'  AND  '2012-12-24 10:00:00'
          OR (a.start < '2012-12-24 08:30:00' AND a.end > '2012-12-24 10:00:00')
        )
于 2012-12-26T13:14:41.850 回答
0

Don't use MySQL's TIMESTAMP data type; use TIME instead:

MySQL retrieves and displays TIME values in 'HH:MM:SS' format (or 'HHH:MM:SS' format for large hours values). TIME values may range from '-838:59:59' to '838:59:59'. The hours part may be so large because the TIME type can be used not only to represent a time of day (which must be less than 24 hours), but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative).

Therefore:

ALTER TABLE ios_workinghours
  MODIFY start TIME,
  MODIFY end   TIME
于 2012-12-26T13:15:17.657 回答
0

用于CURTIME()在表中插入当前时间。

根据函数是在字符串还是数字上下文中使用,以 'HH:MM:SS' 或 HHMMSS.uuuuuu 格式返回当前时间。该值以当前时区表示。

此外,您可以参考

于 2012-12-26T13:07:42.880 回答
0

试试这个它会解决你的问题-

select * 
from ios_appointments a join ios_workinghours h using(workerid_fk)
where workerid_fk = 1
AND date_format( h.start, "%h:%i:%s")  <= '08:30:00' AND date_format( h.end, "%h:%i:%s") >= '10:00:00'
AND (
    a.start BETWEEN '2012-12-24 08:30:00'  AND  '2012-12-24 10:00:00'
    OR  a.end   BETWEEN '2012-12-24 08:30:00'  AND  '2012-12-24 10:00:00'
    OR (a.start < '2012-12-24 08:30:00' AND a.end > '2012-12-24 10:00:00')
)
于 2012-12-26T13:17:30.690 回答