生成一个mysql BETWEEN子句以在日期范围内查找

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成一个mysql BETWEEN子句以在日期范围内查找相关的知识,希望对你有一定的参考价值。

requires Rails libraries. could be rewritten to just use core Ruby libs, but I'm too lazy right now and I use this entirely in my Rails apps.
  1. def mysql_date_compare(start_date, end_date, col_name = :created_at)
  2. range_start = (Date.parse(start_date.to_s) - 1.day).strftime("%Y-%m-%d 23:59:59")
  3. range_end = (Date.parse(end_date.to_s) + 1.day).strftime("%Y-%m-%d 00:00:00")
  4. "#{col_name.to_s} BETWEEN '#{range_start}' AND '#{range_end}'"
  5. end

以上是关于生成一个mysql BETWEEN子句以在日期范围内查找的主要内容,如果未能解决你的问题,请参考以下文章

MySQL 用 BETWEEN AND 日期查询包含范围边界

MySQL 用 BETWEEN AND 日期查询包含范围边界

Oracle Between(范围)子句

Oracle Between(范围)子句

BETWEEN 操作符

Oracle Between子句