Pregunta

need your help. I tried myself but cant solve it.

I have a table with date column (ex. birth) So i need to select all rows where birth is in past more than one months.

I googled and found a SO question similar The difference in months between dates in MySQL

But there is no a condition by this new period. My wrong query is:

select id,period_diff(date_format(now(), "%Y%m"), date_format(birth, "%Y%m")) as months 
from t1
where months>0 
limit 10
¿Fue útil?

Solución

Try Below:

use having (ALIAS months in WHERE Clause not working here)

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

HAVING is like WHERE but it is able to work on columns which are computed (Alias). HAVING works by pruning results after the rest of the query has been run - it is not a substitute for the WHERE clause.

Otros consejos

you cannot use ALIAS in WHERE Clause

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top