mysql as子句为表取名_mysql AS子句

bd96500e110b49cbb3cd949968f18be7.png

I have written a query, and from the date of birth field, and I working out the age of a person and then using AS age to create an age field.

My question is, is it possible to the match again that age field?

Something like this,

SELECT `candidates`.`candidate_id`,

`candidates`.`first_name`,

`candidates`.`surname`,

`candidates`.`DOB`,

`candidates`.`gender`,

DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(`candidates`.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(`candidates`.`DOB`, '00-%m-%d')) AS `age`

FROM `candidates`

WHERE `age` <= 20

Any help would be greatly appreciated?

-----So I have changed WHERE to HAVING and here is my full query------

SELECT `candidates`.`candidate_id`,

`candidates`.`first_name`,

`candidates`.`surname`,

`candidates`.`DOB`,

`candidates`.`gender`,

`candidates`.`talent`,

`candidates`.`location`,

`candidates`.`availability`,

`candidate_assets`.`url`,

`candidate_assets`.`asset_size`

FROM `candidates`

LEFT JOIN `candidate_assets` ON `candidate_assets`.`candidates_candidate_id` = `C`.`candidate_id`

WHERE `C`.`availability` = 'yes'

AND C.talent = "actor"

AND C.skill = "accents"

AND C.gender = "male"

AND HAVING DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(`candidates`.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(`candidates`.`DOB`, '00-%m-%d')) <= 69

AND HAVING DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(`candidates`.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(`candidates`.`DOB`, '00-%m-%d')) <=19

AND HAVING DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(`candidates`.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(`candidates`.`DOB`, '00-%m-%d')) <=49

I get the following error,

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'HAVING DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(candidates.DOB, '%Y') - (DATE_' at line 15

For the life of me I have no idea what it is.

解决方案

One way to do this is to wrap the original query as a subquery, and move the WHERE clause to the outer query:

SELECT * FROM (

SELECT `candidates`.`candidate_id`, -- this is the original query

`candidates`.`first_name`,

`candidates`.`surname`,

`candidates`.`DOB`,

`candidates`.`gender`,

DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(`candidates`.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(`candidates`.`DOB`, '00-%m-%d')) AS `age`

FROM `candidates` ) as innertable

WHERE `age` <= 20 -- this is now part of the outer query

clarification: this DOES work in MySQL 5

note: this assumes that the original query works

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值