Mysql DATE_SUB()始终返回0个结果
我正在尝试从数据库表中选择2周前的所有行Mysql DATE_SUB()始终返回0个结果,mysql,select,Mysql,Select,我正在尝试从数据库表中选择2周前的所有行 date\u updated字段是一个unix时间戳,这就是我在查询中使用的时间戳 以下是查询: select * from `leads` where date_format(from_unixtime(date_updated), '%Y-%m-%d') = date_format(date_sub(now(), interval 2 week), '%Y-%m-%d') 为什么尽管表中有2周的潜在客户,但此查询每次返回0个结果?在查询中更新日期。
date\u updated
字段是一个unix时间戳,这就是我在查询中使用的时间戳
以下是查询:
select * from `leads` where date_format(from_unixtime(date_updated), '%Y-%m-%d') = date_format(date_sub(now(), interval 2 week), '%Y-%m-%d')
为什么尽管表中有2周的潜在客户,但此查询每次返回0个结果?在查询中更新日期。\u sub()。按以下格式更改查询
select * from `leads` where date_format(from_unixtime(date_updated), '%Y-%m-%d') = date_format(date_sub(now() - interval 2 week), '%Y-%m-%d')
出于性能原因,我会在执行查询之前计算日期:
select * from `leads` where date_format(from_unixtime(date_updated), '%Y-%m-%d') = date_format(date_sub(now(), interval 2 week), '%Y-%m-%d')
PHP:
你能给我们一个sqlfiddle吗?样本数据和期望的结果会有所帮助。我最好的猜测是,您的字段并不是MySQL所理解的真正的unix时间戳。问题是我在执行查询之前就返回了一个类方法。谢谢大家的帮助。这是一个优化和加速查询的好方法。谢谢你。