简体   繁体   中英

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. In my MySQL DB I have two timestamp fields. 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.

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. 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

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; 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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