1

I have a MySQL table named tasks that contains four columns:

id (Integer)
name (Varchar)
task_start (DateTime)
task_finish (DateTime)

How can I write an SQL statement that returns tasks that are running at a specific point in time (e.g. task that are running right now)?

2 Answers 2

3

You can do it this way

-- Tasks that were running at specific (e.g. 2014-02-01 12:15:00) point in time 
SELECT id, name, task_start, task_finish
  FROM tasks
 WHERE '2014-02-01 12:15:00' BETWEEN task_start AND task_finish;

-- Tasks that are running right now
SELECT id, name, task_start, task_finish
  FROM tasks
 WHERE NOW() BETWEEN task_start AND task_finish;

Here is SQLFiddle demo

0

task that are now running

select * from tasks where task_start is not null and task_finish is null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.