[英]MYSql Stored Procedure, DateTime value not working
DELIMITER //
CREATE PROCEDURE GetWinners(IN Number INTEGER , IN Table_Name VarChar(7))
BEGIN
SET @t1 = CONCAT('Select * FROM ', Table_Name, ' Where number = ', Number, ' && active = true && dateAndTime < 2015-02-10');
PREPARE stmt3 FROM @t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
END //
DELIMITER ;
dateAndTime is a column where I store DateTime values. dateAndTime是我存储DateTime值的列。 I can't understand why with
&& dateAndTime < 2015-02-10'
, the query returns no results. 我不明白为什么
&& dateAndTime < 2015-02-10'
,查询没有返回结果。
If I remove && dateAndTime < 2015-02-10'
, then some results are returned. 如果我删除
&& dateAndTime < 2015-02-10'
,则会返回一些结果。 This happens even though I have DateTime value lower than 2015-02-10 in the columns. 即使我在列中的DateTime值小于2015-02-10,也会发生这种情况。
This most probably due to your date format, the time is added to the date but due to your preferences in mysql, it only shows the date without time. 这很可能是由于您的日期格式,将时间添加到了日期,但是由于您在mysql中的首选项,它仅显示了没有时间的日期。 Use
trim
or trunc
to remove the time. 使用
trim
或trunc
删除时间。
TRUNC(dateAndTime) < TO_DATE('2015-02-10', 'YYYY-MM-DD')
用单引号括住您的DateTime
SET @t1 = CONCAT('Select * FROM ', Table_Name, ' Where number = ', Number, ' && active = true && dateAndTime < '2015-02-10'');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.