J
J
John Freeman2018-03-02 15:25:13
MySQL
John Freeman, 2018-03-02 15:25:13

How to do date check?

Good day, I have a question:
there is a table:

id  |  name  |  start_date  |  end_date
1   |  act1    |  2018-02-15 00:00:00  |  2018-04-05 00:00:00
1   |  act2    |  NULL  |  2018-04-23 00:00:00
1   |  act3    |  2018-02-12 00:00:00  |  2018-02-28 00:00:00
1   |  act4    |  2018-03-11 00:00:00  |  2018-04-05 00:00:00
1   |  act5    |  2018-02-15 00:00:00  |  2018-02-20 00:00:00

this is a table of events,
how can I correctly draw conclusions from it if start_date is empty or has already come, while end_date has not come, then this is Active Action , if start_date has not come, then this is Upcoming Action , if end_date has already passed, then this is Ended Action
no solution found!
Thanks in advance for your reply!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lander, 2018-03-02
@AsviS

SELECT 'Active Action' as `type`, * FROM `table` WHERE `start_date` IS NULL OR (NOW() BETWEEN `start_date` AND `end_date`)
UNION
SELECT 'Upcoming Action' as `type`, * FROM `table` WHERE `start_date` > NOW();

Something like this ...
upd: I did not check it myself, maybe there is an error somewhere, but I conveyed the essence.

S
Sergey Gerasimov, 2018-03-02
@mrTeo

Try using if or case like this:

SELECT CASE <variable> WHEN <value>      THEN <returnvalue>
                       WHEN <othervalue> THEN <returnthis>
                                         ELSE <returndefaultcase>
       END AS <newcolumnname>
FROM <table>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question