MySQL Query to get age from date of birth

mysql

Solution

You can not use column aliases in WHERE clauses: http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html.

You will have to rethink your query or change to:

SELECT id, pseudo, nom, prenom, sexe, ville, 
   FLOOR(DATEDIFF (NOW(), date_naissance)/365) AS mAge 
FROM user 
WHERE sexe = 'Homme' AND 
FLOOR(DATEDIFF (NOW(), date_naissance)/365) BETWEEN 18 AND 25 
OR ville = 'Bordeaux'

Ps you may also want to have a look at your ANDs and ORs as well as you might want to include some brackets.

Problem

I have an issue with my query, ``` SELECT id, pseudo, nom, prenom, sexe, ville, FLOOR(DATEDIFF (NOW(), date_naissance)/365) AS mAge FROM user WHERE sexe = 'Homme' AND mAge BETWEEN 18 AND 25 OR ville = 'Bordeaux' ``` It is supposed to return the matching user with the where condition. The problem is the following, mAge is not existing I get the following error : 1054 - Unknown column 'mAge' in 'where clause' Looks like my alias is not working properly on the where condition. If I remove the mAge WHERE CLAUSE, I successfully get the mAge alias. I need you guys Thanks in advance !

Original source

Related problems