good day

Help form a request. There is a bill table (MySQL) in it the following fields:

  • 1: id
  • 2: id_importance
  • 3: date_start

It is necessary to select the entire record in a certain range in the date_start field (say +/- 7 days), but if the id_importance record is 7, then we select them in a different range (say +/- 20 days).

All this must be done in a single request. Is it possible?

  • one
    Can. use UNION or UNION ALL .... that is, in general, the query will be as (SELECT ... WHERE id_importance != 7) UNION (SELECT ...... WHERE id_importance = 7) - Alexey Shimansky
  • Thanks for the idea, I adapted it in the framework and it all worked. - SOFQ3

1 answer 1

across the date_start field (say +/- 7 days), but if the id_importance record is 7, then we select them in a different range (say +/- 20 days).

 WHERE date_start BETWEEN @date - INTERVAL CASE WHEN id_importance=7 THEN 20 ELSE 7 END DAY AND @date + INTERVAL CASE WHEN id_importance=7 THEN 20 ELSE 7 END DAY