MySQL日期比较问题?

时间:2010-12-13 05:37:05

标签: mysql

SELECT * FROM shortleavedetails WHERE employee_code='17' AND 
(DATE(authorizeddate) <= DATE(2011-1-1) AND DATE(authorizeddate) >= DATE(2010-5-1))

4 个答案:

答案 0 :(得分:2)

将您的日期值放在单引号中,因为它们是文字字符串,否则MySQL无法判断这是一个日期而不是数字(2011 MINUS 1 MINUS 1 = 2009)。

您不需要在它们周围投射DATE()。

SELECT 
  *
FROM
  shortleavedetails
WHERE
  employee_code = '17'
AND
  (DATE(authorizeddate) <= '2011-01-01'
   AND
   DATE(authorizeddate) >= '2010-05-01')

答案 1 :(得分:2)

试试这个

SELECT * FROM shortleavedetails WHERE employee_code='17' AND 
(authorizeddate<='2011-1-1' AND authorizeddate >= '2010-5-1')

答案 2 :(得分:1)

SELECT * FROM shortleavedetails WHERE employee_code='17' AND 
authorizeddate <= '2011-01-01' AND authorizeddate >='2010-05-01'

答案 3 :(得分:1)

SELECT * FROM shortleavedetails WHERE employee_code='17' AND  authorizeddate between '2010-5-1'and '2011-1-1'