R
R
Roman2021-05-13 13:40:26
MySQL
Roman, 2021-05-13 13:40:26

How to select data from mysql table by multiple dates?

I inherited a site on DLE and there is a self-written plugin that allows you to display episodes for the series. Among other things, there is such a terrible request.

(SELECT `t1`.`season`, `t1`.`episode`, `t1`.`name_orig`, `t1`.`name_rus`, `t1`.`date` AS `release`, `t2`.`id`, `t2`.`alt_name`, `t2`.`date`, `t2`.`category` 
                        FROM `" . PREFIX . "_episodes` AS `t1` 
                        LEFT OUTER JOIN `" . PREFIX . "_post` AS `t2` ON `t2`.`id` = `t1`.`post_id` 
                        WHERE `t1`.`date` <= NOW()
                        ORDER BY `season`, `episode` DESC
                        " . $limitSQL . ")
                    UNION
                        (SELECT `t1`.`season`, `t1`.`episode`, `t1`.`name_orig`, `t1`.`name_rus`, `t1`.`date` AS `release`, `t2`.`id`, `t2`.`alt_name`, `t2`.`date`, `t2`.`category` 
                        FROM `" . PREFIX . "_episodes` AS `t1` 
                        LEFT OUTER JOIN `" . PREFIX . "_post` AS `t2` ON `t2`.`id` = `t1`.`post_id` 
                        WHERE `t1`.`date` > NOW()
                        ORDER BY `season`, `episode`
                        LIMIT 1)


As conceived by the author, this query should display 3 previous episodes and one next from the current date.
Please advise how this request can be improved. I guess it's easier to choose the right data

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question