本文介绍了MySQL查询以获取出生日期的年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的查询有问题

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'

应该以条件条件返回匹配的用户.问题如下,mAge不存在,出现以下错误:

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 :

看起来我的别名在where条件下无法正常工作.

Looks like my alias is not working properly on the where condition.

如果我删除了mAge WHERE条款,那么我将成功获得mAge别名.

If I remove the mAge WHERE CLAUSE, I successfully get the mAge alias.

我需要你们

提前谢谢!

推荐答案

您不能在WHERE子句中使用列别名:.

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您可能还希望查看AND和OR,以及可能需要包含一些括号的信息.

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

这篇关于MySQL查询以获取出生日期的年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 06:05