gpt4 book ai didi

mysql - SQL 将所有内容打印在一行中

转载 作者:行者123 更新时间:2023-11-29 16:19:54 27 4
gpt4 key购买 nike

我想分两列打印数据,例如:计数类别

但是我的代码将所有内容打印在一行中,例如:计数类别 计数类别

有什么建议吗?

Select sum(Case when population >= 1000000 and population < 5000000 then 1 else 0 end) as Count, '1 000 000 - 4 999 999' as Category,
sum(Case when population >= 500000 and population < 100000 then 1 else 1 end) as Count, '100 000 - 499 999' as Category,
sum(Case when population >= 500000 and population < 100000 then 1 else 1 end) as Count, '500 000 - 999 999' as Category,
sum(Case when population >= 500000 and population < 100000 then 1 else 1 end) as Count, 'Under 100 000' as Category,
sum(Case when population >= 500000 and population < 100000 then 1 else 1 end) as Count, 'Over 5 million' as Category
from cities

最佳答案

Select sum(Case when population >= 1000000 and population < 5000000 then 1 else 0 end) as Count, '1 000 000 - 4 999 999' as Category FROM cities
UNION
SELECT sum(Case when population >= 500000 and population < 100000 then 1 else 1 end) as Count, '100 000 - 499 999' as Category FROM cities

等等...

使用 UNION 关键字将所有单个结果连接到一个结果表中

如果你只想计算城市,那么你的 else in case 部分应该到处都是 0 而不是 1...无论如何,更好的解决方案是

SELECT COUNT(cities.primKey) as Count, '1 000 000 - 4 999 999' as Category FROM cities WHERE population >= 1000000 and population < 5000000
UNION
SELECT COUNT(cities.primKey) as Count, '1 000 000 - 4 999 999' as Category FROM cities WHERE population >= 500000 and population < 100000
UNION ...

关于mysql - SQL 将所有内容打印在一行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54575899/

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