gpt4 book ai didi

mysql - 在SQL中将多个查询组合成一个查询

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:29 25 4
gpt4 key购买 nike

我在 sql 中遇到问题,因为我对此很陌生。

我有三个问题

select count(*) as range1_50to60 from Customers where age between 50 and 60;

select count(*) as range2_30to40 from Customers where age between 30 and 40;

select count(*) as range3_20to30 from Customers where age between 20 and 30;

有什么方法可以将这些查询组合成一个查询。

问候

萨米拉

最佳答案

使用case表达式做条件聚合:

select count(case when age between 50 and 60 then 1 end) as range1_50to60,
count(case when age between 30 and 40 then 1 end) as range2_30to40,
count(case when age between 20 and 30 then 1 end) as range3_20to30
from Customers

where age between 20 and 60

WHERE 子句并不是真正需要的,但可能会加快速度!

关于mysql - 在SQL中将多个查询组合成一个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48626340/

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