我研究过这段代码:
select *,
year(curdate())-year(dbirth) - (right(curdate(),5) < right(dbirth,5)) as age
from resident_info
having age = 18;
但我想要的是在获得所有已经 18 的信息之后,我想计算它们。有什么办法可以同时输入两个查询或任何解决方案来解决我的问题?非常感谢
除非我遗漏了什么,如果你需要count
这些,那么你可以使用:
select count(*)
from
(
select *,
year(curdate())-year(dbirth) - (right(curdate(),5) < right(dbirth,5)) as age
from resident_info
having age = 18
) src
如果要从resident_info
和中返回所有数据count
,则可以使用CROSS JOIN
:
select *,
year(curdate())-year(dbirth) - (right(curdate(),5) < right(dbirth,5)) as age,
src.Total
from resident_info
cross join
(
select count(*) Total
from
(
select year(curdate())-year(dbirth) - (right(curdate(),5) < right(dbirth,5)) as age
from resident_info
having age = 18
) x
) src
having age = 18
这可能会有所帮助。
select temp.*,
year(curdate())-year(dbirth) - (right(curdate(),5) < right(dbirth,5)) as age
FROM resident_info AS temp
WHERE age = 18;