我希望按照日期和月份范围进行查询,以使用knex获取生日。考虑这个间隔(MM-dd) 03-28和04-03得到所有年份的出生间隔月和日的所有数据。
const query = this.db("user_table")
let dayIni = format(sub(new Date(), { days: 7 }), "dd")
let dayEnd = format(new Date(), "dd")
let monthIni = format(sub(new Date(), { days: 7 }), "MM")
let monthEnd = format(new Date(), "MM")
query.whereRaw("EXTRACT(DAY FROM (user_table.birth)) >= ? AND EXTRACT(MONTH FROM (user_table.birth)) >= ?", [dayIni, monthIni])
query.whereRaw("EXTRACT(DAY FROM (user_table.birth)) <= ? AND EXTRACT(MONTH FROM (user_table.birth)) <= ?", [dayEnd, monthEnd])
暂定答案。在原始SQL中,类似于:
select
*
from
user_table
where
(extract(month from user_table.birth), extract(day from user_table.birth))
between (3, 28) and (4, 3)