首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将多行放入具有不同列的单行中

如何将多行放入具有不同列的单行中
EN

Stack Overflow用户
提问于 2015-10-29 06:05:30
回答 2查看 29关注 0票数 1

我的表结构如下所示,预期的输出如下所示:

性别-计数值

男-9

女-4

预期输出:

男-女

9-4

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-29 06:26:52

你可以这样做:

代码语言:javascript
运行
复制
SELECT  
        max(case when `gender` = 'Male' then countvalue end) as Male,
        max(case when `gender` = 'Female' then countvalue end) as Female
FROM    test 

SQL小提琴演示

票数 1
EN

Stack Overflow用户

发布于 2015-10-29 06:34:07

代码语言:javascript
运行
复制
create table k
(   gender varchar(20) not null,
    theCount int not null
);
insert k(gender,theCount) values ('male',9),('female',4);


select a.theCount as male, b.theCount as female 
from k a 
cross join k b 
where a.gender='male' and b.gender='female';

+------+--------+
| male | female |
+------+--------+
|    9 |      4 |
+------+--------+

交叉连接是笛卡尔积。但是1行接1行就是结果集中的1行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33407026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档