需要你的帮助。我试过自己但无法解决。
我有一个包含日期列(例如出生)的表格,所以我需要选择过去一个多月内出生的所有行。
我用谷歌搜索并发现了一个类似的问题 The difference in months between dates in MySQL
但是这个新时期没有条件。我的错误查询是:
select id,period_diff(date_format(now(), "%Y%m"), date_format(birth, "%Y%m")) as months
from t1
where months>0
limit 10
最佳答案
尝试以下:
使用 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
子句。
关于mysql 月份差异作为 where 子句中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8906962/