gpt4 book ai didi

mysql - 迭代计数和 where 子句

转载 作者:行者123 更新时间:2023-11-29 18:10:23 24 4
gpt4 key购买 nike

我需要一些帮助。我有很多用户及其出生日期:

SELECT COUNT(or SUM) ()    FROM ()

WHERE () = ''

AND TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE())=18(age)

我必须通过仅更改 where 子句中的年龄来多次计算列的计数、总和、平均值等。如何确保我不会浪费时间多次运行查询?我必须从 18 岁升至 65 岁:

SELECT COUNT(or SUM) () FROM ()

WHERE () = '' AND () = ''

AND ()<''

AND TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE())=**19,29,21 etc**.

I would like this columns results:

count() 18

count() 19 etc

or

sum () 18

sum() 19 etc

真的非常感谢。

最佳答案

TIMESTAMPDIFF的结果作为列返回,然后使用GROUP BY

SELECT TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS age, COUNT(*)
FROM ...
WHERE ...
GROUP BY age

关于mysql - 迭代计数和 where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47462248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com