N
N
Nurdaulet Maksutov2021-02-14 15:16:58
SQL
Nurdaulet Maksutov, 2021-02-14 15:16:58

How to implement SQL query on time?

I have a database with tables (id, start, end) .

These are the lessons, start is the beginning of the lesson end the end of the lesson (HH:MM:SS).
Now how to implement a selection to find out what lesson is now, if it is now, for example, 10:25. (+ find out how many minutes until the end of the lesson left). I don't really have any idea how to do it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-02-14
@maksutovn

Comparing with NOW() is not correct because NOW() is the current date + time . If time is stored in the table, use the CURRENT_TIME variable.

select 
  id, 
  title,
  start,
  end,
  timediff(end, current_time) as remain_time
from lessons
where current_time between start and end
;

MariaDB fiddle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question