简体   繁体   中英

sql query with current day comparison

I have a mysql table with a due_date field which is simply an integer value.

dealID   |  due_day
1        |   15
2        |   25
3        |   10
4        |    9
5        |   31
6        |   20 

I would like to query this table to only display the data that would be 14 days before the due_day. For example, today is 01/05/13, if I query this table it should only show me dealID 1, 3 and 9. How should I go about this condition?

You can do that simply using DATE_SUB to substract the number of days from current date and then DAYOFMONTH to get the day.

You can create the query using the mentioned functions.

So based on user1951544's answer this is what I came up with.

    SELECT due_day
    FROM deals
    WHERE (due_day - DAYOFMONTH( NOW( ) ) ) <=14

Query:

SQLFIDDLEExample

SELECT 
dealID, 
due_day
FROM Table1
WHERE due_day  < 14 + DAYOFMONTH( NOW( ) )

Result:

| DEALID | DUE_DAY |
--------------------
|      1 |      15 |
|      3 |      10 |
|      4 |       9 |

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