0

需要你的帮助。我尝试了自己,但无法解决。

我有一个带有日期列的表(例如出生)所以我需要选择出生在过去一个多月内的所有行。

我用谷歌搜索了一个类似 的 SO 问题 MySQL 中日期之间的月份差异

但是这个新时期没有条件。我的错误查询是:

select id,period_diff(date_format(now(), "%Y%m"), date_format(birth, "%Y%m")) as months 
from t1
where months>0 
limit 10
4

2 回答 2

2

试试下面:

使用having(条款ALIAS中的几个月在WHERE这里不起作用)

select id,period_diff(date_format(now(), "%Y%m"), date_format(birth, "%Y%m")) as months 
from t1
having months>0 
limit 10

HAVING类似WHERE,但它能够在计算的列上工作 ( Alias)。HAVING通过在查询的其余部分运行后修剪结果来工作 - 它不能替代WHERE子句。

于 2012-01-18T08:12:35.483 回答
2

你不能ALIAS在 WHERE 子句中使用

SELECT id,
       period_diff(date_format(now(), "%Y%m"), date_format(birth, "%Y%m")) as months 
FROM   t1
WHERE period_diff(date_format(now(), "%Y%m"), date_format(birth, "%Y%m")) > 0 
LIMIT 10
于 2012-01-18T08:15:18.167 回答