简体   繁体   中英

MySQL Query how to show opposite results

im using this SQL Query in my PHP code:

SELECT
   *
FROM
   maintenance
WHERE
   from_date <= DATE_ADD(NOW(), INTERVAL 5 DAY)
   AND to_date >= DATE(NOW())
ORDER BY 
   from_date ASC

its showing rows 5 days before the from_date field. so basically rows where the maintenance is still pending as the date hasn't passed yet.

i want to be able show the oposite results. so all the rows where the maintenance is completed if possible?

You can exclude these results:

SELECT
   * 
FROM
   maintenance
WHERE id NOT IN (SELECT
                   id
                 FROM
                   maintenance
                 WHERE
                   from_date <= DATE_ADD(NOW(), INTERVAL 5 DAY)
                   AND to_date >= DATE(NOW())
                 )
ORDER BY 
   from_date ASC

Or simply, negate the condition in your original query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM