M
M
maxeee522020-01-16 18:37:39
SQL
maxeee52, 2020-01-16 18:37:39

How to build a SQL query to get shares depending on the end date?

Figuratively, there should be 2 parts: active and inactive.
It is necessary to fit everything into one request.
Example of received data:
End date (Just ascending dates starting from today):

  • today
  • tomorrow
  • a week later
  • in a month

(and in the same issue you need to get past promotions)
(Just descending dates starting from today):
  • yesterday
  • the day before yesterday
  • a week ago

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
maxeee52, 2020-01-16
@maxeee52

Wouldn't like to use

isActive
, because this field will have to be updated every time when accessing promotions
At the moment, I googled this option
SELECT * FROM special WHERE date_end > NOW()
UNION
SELECT * FROM special WHERE  date_end < NOW()

And it seems to even solve the sorting problem, although it is not set here, but the output is correct
5e208c7c9b8f5472331937.jpeg

S
Sergey Sokolov, 2020-01-16
@sergiks

For activity / inactivity, add a calculated field to the query output: DATE(NOW()) <= date_end AS isActive
To group by yesterday / the day before yesterday, you will have to sequentially compare the date with

CASE TRUE
WHEN date_end >= DATE_ADD(NOW(), INTERVAL 1 WEEK))
  AND date_end < DATE_ADD(NOW(), INTERVAL 1 MONTH))  THEN "через месяц"
WHEN date_end >= DATE_ADD(NOW(), INTERVAL 1 DAY))
  AND date_end < DATE_ADD(NOW(), INTERVAL 1 WEEK))  THEN "через неделю"
...
END AS kogda
and similar

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question