Mysql学习——行与列的多种转换

目录

一、行转列

二、列转行

三、多列转一行

四、一行转多列

五、行转列的其案例


首先准备一张表

CREATE TABLE CJ
(
    Name    varchar(32),
    Subject varchar(32),
    Result  int(10)
);

# 插入数据
insert into cj
values ('张三', '语文', 80),
       ('张三', '数学', 90),
       ('张三', '物理', 85),
       ('李四', '语文', 85),
       ('李四', '数学', 92),
       ('李四', '物理', 82);

Mysql学习——行与列的多种转换_第1张图片

一、行转列

第一步,先将科目分类好:

SELECT Name,
       CASE WHEN Subject='语文' THEN Result ELSE 0 END AS 语文,
       CASE WHEN Subject='数学' THEN Result ELSE 0 END AS 数学,
       CASE WHEN Subject='数学' THEN Result ELSE 0 END AS 物理
FROM cj;

Mysql学习——行与列的多种转换_第2张图片

第二步:将上面的结果看做一张表,从表中找出每一个新字段的最大值,对Name进行分组

SELECT T.Name,
       MAX(T.语文) 语文,
       MAX(T.数学) 数学,
       MAX(T.物理) 物理
FROM (SELECT Name,
       CASE WHEN Subject='语文' THEN Result ELSE 0 END AS 语文,
       CASE WHEN Subject='数学' THEN Result ELSE 0 END AS 数学,
       CASE WHEN Subject='数学' THEN Result ELSE 0 END AS 物理
FROM cj) T
GROUP BY T.Name;

Mysql学习——行与列的多种转换_第3张图片

案例二:查询用户安装APP的情况

create table app
(
    id  int,
    app varchar(32)
);

insert into app(id, app)
VALUES (1, '微信'),
       (2, '快手'),
       (3, 'QQ'),
       (4, '抖音'),
       (5, '美团'),
       (6, '饿了么'),
       (7, '支付宝'),
       (8, '拼多多'),
       (9, '高德地图');

CREATE TABLE app_install
(
    uid int,
    app varchar(32)
);

insert into app_install(uid, app)
VALUES (1, '微信'),
       (1, '美团'),
       (2, '支付宝'),
       (2, '高德地图'),
       (3, '拼多多');

select uid,
       case when app = '微信' then 1 else 0 end as 'wx',
       case when app = '快手' then 1 else 0 end as 'ks',
       case when app = 'QQ' then 1 else 0 end as 'qq',
       case when app = '抖音' then 1 else 0 end as 'dy',
       case when app = '美团' then 1 else 0 end as 'mt',
       case when app = '饿了么' then 1 else 0 end as 'elm',
       case when app = '支付宝' then 1 else 0 end as 'zfb',
       case when app = '拼多多' then 1 else 0 end as 'pdd',
       case when app = '高德地图' then 1 else 0 end as 'gd'
from app_install;

select t.uid,
       case when max(t.wx) then '已安装' else '未安装' end as 'wx',
       case when max(t.ks) then '已安装' else '未安装' end as 'ks',
       case when max(t.qq) then '已安装' else '未安装' end as 'qq',
       case when max(t.dy) then '已安装' else '未安装' end as 'dy',
       case when max(t.mt) then '已安装' else '未安装' end as 'mt',
       case when max(t.elm) then '已安装' else '未安装' end as 'eml',
       case when max(t.zfb) then '已安装' else '未安装' end as 'zfb',
       case when max(t.pdd) then '已安装' else '未安装' end as 'pdd',
       case when max(t.gd) then '已安装' else '未安装' end as 'gd'
from  (select uid,
       case when app = '微信' then 1 else 0 end as 'wx',
       case when app = '快手' then 1 else 0 end as 'ks',
       case when app = 'QQ' then 1 else 0 end as 'qq',
       case when app = '抖音' then 1 else 0 end as 'dy',
       case when app = '美团' then 1 else 0 end as 'mt',
       case when app = '饿了么' then 1 else 0 end as 'elm',
       case when app = '支付宝' then 1 else 0 end as 'zfb',
       case when app = '拼多多' then 1 else 0 end as 'pdd',
       case when app = '高德地图' then 1 else 0 end as 'gd'
from app_install) t
group by t.uid;

连表比子查询要好  

Mysql学习——行与列的多种转换_第4张图片

二、列转行

建表

CREATE TABLE CJ2
(
    Name varchar(32),
    `语文` int(10),
    `数学` int(10),
    `物理` int(10)
);


# 插入数据
insert into cj2 values ('张三',80,90,90),('李四',85,92,92);

原表:

Mysql学习——行与列的多种转换_第5张图片

SELECT Name,'语文' cource,语文 result
FROM cj2
union all
SELECT Name,'数学' cource,数学 result
FROM cj2
union all
SELECT Name,'物理' cource,物理 result
FROM cj2;

# 查询后按照结果排序
SELECT *
FROM (SELECT Name,'语文' cource,语文 result
FROM cj2
union all
SELECT Name,'数学' cource,数学 result
FROM cj2
union all
SELECT Name,'物理' cource,物理 result
FROM cj2) T
ORDER BY T.Name;

Mysql学习——行与列的多种转换_第6张图片

三、多列转一行

将科目与分数排在一列

SELECT Name,GROUP_CONCAT(Subject,':',Result) 成绩
FROM cj
group by Name;

 Mysql学习——行与列的多种转换_第7张图片

四、一行转多列

将上表还原

# 建表
CREATE TABLE CJ3
(
    Name varchar(32),
    `成绩` varchar(50)
);

# 插入数据
insert into cj3
values ('张三', '语文:80,数学:90,物理:85'),
       ('李四', '语文:85,数学:92,物理:82');

Mysql学习——行与列的多种转换_第8张图片

SELECT Name,
       CASE
           WHEN LOCATE('语文', 成绩) > 0 THEN SUBSTRING_INDEX(SUBSTRING_INDEX(成绩, '语文:', -1), ',', 1)
           else 0 end as 语文,
       CASE
           WHEN LOCATE('数学', 成绩) > 0 THEN SUBSTRING_INDEX(SUBSTRING_INDEX(成绩, '数学:', -1), ',', 1)
           else 0 end as 数学,
       CASE
           WHEN LOCATE('物理', 成绩) > 0 THEN SUBSTRING_INDEX(SUBSTRING_INDEX(成绩, '物理:', -1), ',', 1)
           else 0 end as 物理
from cj3;

Mysql学习——行与列的多种转换_第9张图片

SELECT T1.Name, '语文' Cource, T1.语文 result
FROM (SELECT Name,
             CASE
                 WHEN LOCATE('语文', 成绩) > 0 THEN SUBSTRING_INDEX(SUBSTRING_INDEX(成绩, '语文:', -1), ',', 1)
                 else 0 end as 语文
      from cj3) T1
union all
SELECT T2.Name, '数学' Cource, T2.数学 result
FROM (SELECT Name,
             CASE
                 WHEN LOCATE('数学', 成绩) > 0 THEN SUBSTRING_INDEX(SUBSTRING_INDEX(成绩, '数学:', -1), ',', 1)
                 else 0 end as 数学
      from cj3) T2
union all
SELECT T3.Name, '物理' Cource, T3.物理 result
FROM (SELECT Name,
             CASE
                 WHEN LOCATE('物理', 成绩) > 0 THEN SUBSTRING_INDEX(SUBSTRING_INDEX(成绩, '物理:', -1), ',', 1)
                 else 0 end as 物理
      from cj3) T3;

Mysql学习——行与列的多种转换_第10张图片

SELECT *
FROM (SELECT T1.Name, '语文' Cource, T1.语文 result
FROM (SELECT Name,
             CASE
                 WHEN LOCATE('语文', 成绩) > 0 THEN SUBSTRING_INDEX(SUBSTRING_INDEX(成绩, '语文:', -1), ',', 1)
                 else 0 end as 语文
      from cj3) T1
union all
SELECT T2.Name, '数学' Cource, T2.数学 result
FROM (SELECT Name,
             CASE
                 WHEN LOCATE('数学', 成绩) > 0 THEN SUBSTRING_INDEX(SUBSTRING_INDEX(成绩, '数学:', -1), ',', 1)
                 else 0 end as 数学
      from cj3) T2
union all
SELECT T3.Name, '物理' Cource, T3.物理 result
FROM (SELECT Name,
             CASE
                 WHEN LOCATE('物理', 成绩) > 0 THEN SUBSTRING_INDEX(SUBSTRING_INDEX(成绩, '物理:', -1), ',', 1)
                 else 0 end as 物理
      from cj3) T3) T
ORDER BY T.Name;

Mysql学习——行与列的多种转换_第11张图片

五、行转列的其他案例

准备一张result表

Mysql学习——行与列的多种转换_第12张图片

行转列

# 查询1000号学生四门科目的成绩
select StudentNo,
       case when SubjectNo = 1 then StudentResult else 0 end as 高等数学1,
       case when SubjectNo = 2 then StudentResult else 0 end as 高等数学2,
       case when SubjectNo = 3 then StudentResult else 0 end as java编程,
       case when SubjectNo = 4 then StudentResult else 0 end as hadoop理论
from result
where StudentNo = 1000;

Mysql学习——行与列的多种转换_第13张图片

# 简化
select StudentNo, MAX(高等数学1) math1, MAX(高等数学2) math2, MAX(java编程) java, MAX(hadoop理论) hadoop
from (select StudentNo,
             case when SubjectNo = 1 then StudentResult else 0 end as 高等数学1,
             case when SubjectNo = 2 then StudentResult else 0 end as 高等数学2,
             case when SubjectNo = 3 then StudentResult else 0 end as java编程,
             case when SubjectNo = 4 then StudentResult else 0 end as hadoop理论
      from result
      where StudentNo = 1000) T;

你可能感兴趣的:(#,SQL学习,sql,数据库)