简体   繁体   English

仅在select语句中设置时间戳记时间

[英]Only set time of timestamp in select statement

I have an app where the user can define when he is working: eg from 8am to 5pm. 我有一个应用程序,用户可以在其中定义工作时间:例如从上午8点到下午5点。 In my MySQL DB I have two timestamp fields. 在我的MySQL数据库中,我有两个时间戳字段。 How can I only set the time, but not the date? 如何只设置时间而不设置日期? I need this because only the time is important. 我需要这个,因为只有时间很重要。 I just want to know from when to when the user is working. 我只想知道从什么时候到什么时候工作。 Date itself is irrelevant. 日期本身无关紧要。

I tried something like this but my database refuses it: 我尝试过类似的操作,但是我的数据库拒绝了它:

0000-00-00 08:00:00

This is my select statement: 这是我的选择语句:

# 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')
)

Use CURTIME() to insert current time, in your table. 使用CURTIME()在表中插入当前时间。

Returns the current time as a value in 'HH:MM:SS' or HHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. 以'HH:MM:SS'或HHMMSS.uuuuuuu格式返回当前时间作为值,具体取决于该函数是在字符串还是数字上下文中使用。 The value is expressed in the current time zone. 该值以当前时区表示。

Further, you may refer 此外,您可以参考

not sure if this is what your are asking for but u can use DATE_FORMAT 不知道这是否是您要的,但是您可以使用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')
        )

Don't use MySQL's TIMESTAMP data type; 不要使用MySQL的TIMESTAMP数据类型; use TIME instead: 使用TIME代替:

MySQL retrieves and displays TIME values in 'HH:MM:SS' format (or 'HHH:MM:SS' format for large hours values). MySQL检索并以'HH:MM:SS'格式显示TIME值(或对于大型小时值显示为'HHH:MM:SS'格式)。 TIME values may range from '-838:59:59' to '838:59:59' . TIME值的范围可以是'-838:59:59''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). 小时部分可能是如此之大,因为“ TIME类型不仅可以用来表示一天中的某个时间(必须少于24小时),而且可以用来表示经过的时间或两个事件之间的时间间隔(可能远大于24小时,甚至是负面的)。

Therefore: 因此:

ALTER TABLE ios_workinghours
  MODIFY start TIME,
  MODIFY end   TIME

Try this it will solve your problem- 试试这个可以解决您的问题-

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')
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM