Retrieving data for the specified time range is not as expected

Problem background:
Carry out different business logic on different dates, simulate in the test environment, set the date to the time of the day, and distinguish different dates by hours, minutes and seconds;
the data that is not in this time interval is counted in this time interval;

problem location : SQL parameterType = map
in the Mybatis configuration file confirms that the incoming time parameter is a string, the format is yyyy-MM-dd HH:mm:SS, accurate to hours, minutes and seconds, conforming to the datetime type View SQL:




SELECT * FROM `p2p_td_project_invest`
WHERE 1=1
 AND DATE(C_CREATE_TIME) >= DATE("2017-10-27 18:31:00")
 AND DATE(C_CREATE_TIME) <= DATE("2017-10-27 18:45:00")
 ORDER BY C_CREATE_TIME DESC LIMIT 10 ;


DATE function is used
SELECT DATE(C_CREATE_TIME) FROM p2p_td_project_invest ORDER BY C_CREATE_TIME DESC LIMIT 10 ;
-- The query result is yyyy-MM-dd

SELECT DATE("2017-10-27 18:45:00") FROM DUAL ;
-- The query result is yyyy-MM-dd


That is, although the incoming parameter is in yyyy-MM-dd HH:mm:SS format, it becomes yyyy-MM-dd format after DATE processing.

Solution :
SELECT * FROM `p2p_td_project_invest`
WHERE 1=1
 AND C_CREATE_TIME >= DATE("2017-10-10")
 AND C_CREATE_TIME <= DATE("2017-10-19")
 ORDER BY C_CREATE_TIME DESC LIMIT 10 ;
-- Remove the DATE function on the field, the result is correct

or
SELECT * FROM `p2p_td_project_invest`
WHERE 1=1
 AND C_CREATE_TIME >= "2017-10-27 00:00:00"
 AND C_CREATE_TIME <= "2017-10-27 23:59:59"
 ORDER BY C_CREATE_TIME DESC LIMIT 10 ;



But this modification is wrong, C_CREATE_TIME is datetime type, and the unspecified hour, minute and second on the right side of the equal sign is the default 0 hours, 0 minutes and 0 seconds
SELECT * FROM `p2p_td_project_invest`
WHERE 1=1
 AND C_CREATE_TIME >= "2017-10-27"
 AND C_CREATE_TIME <= "2017-10-27"
 ORDER BY C_CREATE_TIME DESC LIMIT 10 ;


http://www.w3school.com.cn/sql/sql_dates.asp
Use the time field for accurate query, it is recommended to only use the date part, you can delay the deadline log by one day, in order to get the time range

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326688558&siteId=291194637