我试图在两个日期之间获取值,特别是24小时
选择 *来自交易在哪里accnum =‘1534610376’ 并且在20190311和20190312之间进行比较此查询工作正常但是,…
交易
我建议你把你的日期放在这样的小数之间:
SELECT * FROM `transactions` WHERE accnum = '1534610376' AND tdate BETWEEN '20190311' AND '20190312'
之后,您可以定义用户定义的函数,如下所示:
CREATE FUNCTION BetweenDate(@toCompare VARCHAR(30), @rightDate DATE, @leftDate DATE) RETURNS TABLE AS BEGIN RETURN ( SELECT * FROM transactions WHERE accum = @toCompare AND tdate BETWEEN @rightDATE AND @leftDate ) END;
如果你“想要从今天开始的记录” - 一个简单的方法是:
WHERE accnum = '1534610376' AND DATE(tdate) = CURRENT_DATE()
但是 - 要使用索引,不应将列包装到函数中。所以一个有效的方法
WHERE accnum = '1534610376' AND tdate >= CURRENT_DATE() AND tdate < CURRENT_DATE() + INTERVAL 1 DAY