简体   繁体   中英

MySQL query to omit records, while searching, after X amount of Days

I am using the following query,

SELECT 
b.sales_id,b.category_id,a.bus_title,b.sales_title,b.sale_starts,b.sale_ends 
FROM tbl_sales b INNER JOIN tbl_business a 
ON a.bus_id=b.bus_id 
WHERE b.active=1 
AND CURDATE( ) < DATE_ADD(b.sale_ends, INTERVAL 14 DAY ) 
AND b.category_id='16' OR b.category_id 
IN (SELECT cat_id FROM tb_category WHERE parent_id=16) 
ORDER BY (b.sale_ends=CURDATE()) DESC,(b.sale_ends>CURDATE()) DESC,b.sale_ends ASC

and getting the result as shows below,

sales_id  | category_id |bus_title   | sales_title|sale_starts|sale_ends 
----------|-------------|------------|------------|-----------|----------
 36       |   17        |  my bus    | my sale    |2012-04-03 |2012-05-03  
 35       |   19        |  my bus 1  | my sale 1  |2012-04-03 |2012-05-03
 42       |   16        |  my bus 12 | my sale 12 |2012-04-05 |2012-05-05
 10       |   17        |  my bus 123| my sale 123|2011-12-15 |2011-12-18

I need to omit(not delete) sales after X amount of days past the End Date (here after 14 days). But the MySQL query i had written above returns the wrong results and displays the records with End Date 2011-12-18 . How can i write the query in an effective way.

Need help. Thanks in advance.

将此添加到您的查询:

   AND sale_ends < DATE_SUB(NOW(), INTERVAL 14 day)

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