需要你的帮助。我尝试了自己,但无法解决。
我有一个带有日期列的表(例如出生)所以我需要选择出生在过去一个多月内的所有行。
我用谷歌搜索了一个类似 的 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
需要你的帮助。我尝试了自己,但无法解决。
我有一个带有日期列的表(例如出生)所以我需要选择出生在过去一个多月内的所有行。
我用谷歌搜索了一个类似 的 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
试试下面:
使用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
子句。
你不能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