将值拆分为其他几列
Split Values into several other columns
代码:
CREATE TABLE table1 (
day DATE,
name VARCHAR(40)
);
INSERT INTO table1 (day, name)
VALUES
('2018-01-01', 'test1'),
('2018-01-01', 'test2'),
('2018-01-01', 'example'),
('2018-01-01', 'somevalue'),
('2018-01-02', 'test3'),
('2018-01-03', 'test4');
我想将结果拆分成如下内容:
day - name1 - name2 - namex
DATE - value - value - value
而不是复制我 select 数据时的日期。
预期结果:
day - name - name - name - name ...
2018-01-01 - test1 - test2 - example - somevalue
2018-01-02 - NULL - NULL - NULL - NULL - test3
在此代码中,您按日期和名称分组并按升序排序
SELECT * FROM table1
group by day, name
ORDER BY day ASC;
检查image
您可以使用 group by 和 group concat 执行此操作,如下所示
select t.day,left(t.data,length(t.data)-1)
from
(
SELECT day,replace(group_concat(concat(name,'-')),',','')as data
FROM table1
group by day
)t
您可以通过动态 sql 执行此操作,首先找到不同的名称值,然后围绕它们构建其余代码
给出
MariaDB [sandbox]> select * from t;
+------------+-----------+
| day | name |
+------------+-----------+
| 2018-01-01 | test |
| 2018-01-01 | test |
| 2018-01-01 | example |
| 2018-01-01 | somevalue |
| 2018-01-02 | test |
| 2018-01-03 | test |
+------------+-----------+
6 rows in set (0.00 sec)
set @sql = concat('select day, ',
(select group_concat(maxstr)
from
(select concat('max(case when name = ', char(39),name,char(39),' then name else null end) as ', concat('name',@rn:=@rn+1)) maxstr
from
(select distinct name from t) t,(select @rn:=0) rn
) s
)
,
' from t group by day')
;
构建此代码
select day, max(case when name = 'test' then name else null end) as name1,
max(case when name = 'example' then name else null end) as name2,
max(case when name = 'somevalue' then name else null end) as name3
from t group by day;
当 运行
时会产生此结果
+------------+-------+---------+-----------+
| day | name1 | name2 | name3 |
+------------+-------+---------+-----------+
| 2018-01-01 | test | example | somevalue |
| 2018-01-02 | test | NULL | NULL |
| 2018-01-03 | test | NULL | NULL |
+------------+-------+---------+-----------+
3 rows in set (0.00 sec)
使用动态 sql 的优点是它非常火,忘记任何新值都会被代码捕获。不过要注意 group_concat 的限制。
像这样执行动态sql-
prepare sqlstmt from @sql;
execute sqlstmt;
deallocate prepare sqlstmt;
代码:
CREATE TABLE table1 (
day DATE,
name VARCHAR(40)
);
INSERT INTO table1 (day, name)
VALUES
('2018-01-01', 'test1'),
('2018-01-01', 'test2'),
('2018-01-01', 'example'),
('2018-01-01', 'somevalue'),
('2018-01-02', 'test3'),
('2018-01-03', 'test4');
我想将结果拆分成如下内容:
day - name1 - name2 - namex
DATE - value - value - value
而不是复制我 select 数据时的日期。
预期结果:
day - name - name - name - name ...
2018-01-01 - test1 - test2 - example - somevalue
2018-01-02 - NULL - NULL - NULL - NULL - test3
在此代码中,您按日期和名称分组并按升序排序
SELECT * FROM table1
group by day, name
ORDER BY day ASC;
检查image
您可以使用 group by 和 group concat 执行此操作,如下所示
select t.day,left(t.data,length(t.data)-1)
from
(
SELECT day,replace(group_concat(concat(name,'-')),',','')as data
FROM table1
group by day
)t
您可以通过动态 sql 执行此操作,首先找到不同的名称值,然后围绕它们构建其余代码
给出
MariaDB [sandbox]> select * from t;
+------------+-----------+
| day | name |
+------------+-----------+
| 2018-01-01 | test |
| 2018-01-01 | test |
| 2018-01-01 | example |
| 2018-01-01 | somevalue |
| 2018-01-02 | test |
| 2018-01-03 | test |
+------------+-----------+
6 rows in set (0.00 sec)
set @sql = concat('select day, ',
(select group_concat(maxstr)
from
(select concat('max(case when name = ', char(39),name,char(39),' then name else null end) as ', concat('name',@rn:=@rn+1)) maxstr
from
(select distinct name from t) t,(select @rn:=0) rn
) s
)
,
' from t group by day')
;
构建此代码
select day, max(case when name = 'test' then name else null end) as name1,
max(case when name = 'example' then name else null end) as name2,
max(case when name = 'somevalue' then name else null end) as name3
from t group by day;
当 运行
时会产生此结果+------------+-------+---------+-----------+
| day | name1 | name2 | name3 |
+------------+-------+---------+-----------+
| 2018-01-01 | test | example | somevalue |
| 2018-01-02 | test | NULL | NULL |
| 2018-01-03 | test | NULL | NULL |
+------------+-------+---------+-----------+
3 rows in set (0.00 sec)
使用动态 sql 的优点是它非常火,忘记任何新值都会被代码捕获。不过要注意 group_concat 的限制。
像这样执行动态sql-
prepare sqlstmt from @sql;
execute sqlstmt;
deallocate prepare sqlstmt;