mysql行转列

mysql行转列_第1张图片
mysql 转储存.sql文件
/*
Navicat MySQL Data Transfer

Source Server : localhost_3306
Source Server Version : 80012
Source Host : localhost:3306
Source Database : test

Target Server Type : MYSQL
Target Server Version : 80012
File Encoding : 65001

Date: 2019-01-17 11:04:54
*/

SET FOREIGN_KEY_CHECKS=0;


– Table structure for test


DROP TABLE IF EXISTS test;
CREATE TABLE test (
aid int(11) NOT NULL,
data varchar(255) DEFAULT NULL,
id int(11) DEFAULT NULL,
sata varchar(255) DEFAULT NULL,
PRIMARY KEY (aid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


– Records of test


INSERT INTO test VALUES (‘1’, ‘201801’, ‘10’, ‘1’);
INSERT INTO test VALUES (‘2’, ‘201801’, ‘11’, ‘2’);
INSERT INTO test VALUES (‘3’, ‘201802’, ‘10’, ‘3’);
INSERT INTO test VALUES (‘4’, ‘201802’, ‘11’, ‘3’);
INSERT INTO test VALUES (‘5’, ‘201803’, ‘10’, ‘1’);
INSERT INTO test VALUES (‘6’, ‘201803’, ‘11’, ‘1’);

查询语句

SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
‘MAX(IF(data = ‘’’,
data,
‘’’, sata, 0)) AS ‘’’,
data, ‘’’’
)
) INTO @sql
FROM test;

SET @sql = CONCAT(‘Select id, ‘, @sql,’ From test group by id’);
PREPARE a FROM @sql;
EXECUTE a;
DEALLOCATE PREPARE a;

知识点:
1、mysql之group_concat函数详解
函数语法:
group_concat( [DISTINCT] 要连接的字段 [Order BY 排序字段 ASC/DESC] [Separator ‘分隔符’] )
下面举例说明:

select * from goods;

±-----±-----+
| id| price|
±-----±-----+
|1 | 10|
|1 | 20|
|1 | 20|
|2 | 20|
|3 | 200 |
|3 | 500 |
±-----±-----+
6 rows in set (0.00 sec)

以id分组,把price字段的值在同一行打印出来,逗号分隔(默认)

select id, group_concat(price) from goods group by id;

±-----±-------------------+
| id| group_concat(price) |
±-----±-------------------+
|1 | 10,20,20|
|2 | 20 |
|3 | 200,500|
±-----±-------------------+
3 rows in set (0.00 sec)

以id分组,把price字段的值在一行打印出来,分号分隔

select id,group_concat(price separator ‘;’) from goods group by id;
±-----±---------------------------------+
| id| group_concat(price separator ‘;’) |
±-----±---------------------------------+
|1 | 10;20;20 |
|2 | 20|
|3 | 200;500 |
±-----±---------------------------------+
3 rows in set (0.00 sec)

以id分组,把去除重复冗余的price字段的值打印在一行,逗号分隔
select id,group_concat(distinct price) from goods group by id;

±-----±----------------------------+
| id| group_concat(distinct price) |
±-----±----------------------------+
|1 | 10,20|
|2 | 20 |
|3 | 200,500 |
±-----±----------------------------+
3 rows in set (0.00 sec)

以id分组,把price字段的值打印在一行,逗号分隔,按照price倒序排列
select id,group_concat(price order by price desc) from goods group by id;

±-----±--------------------------------------+
| id| group_concat(price order by price desc) |
±-----±--------------------------------------+
|1 | 20,20,10 |
|2 | 20|
|3 | 500,200|
±-----±--------------------------------------+
3 rows in set (0.00 sec)

2、MySQL中concat函数(连接字符串)
使用方法:
concat(str1,str2,…)

返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。

mysql> select concat(‘11’,‘22’,‘33’);
±-----------------------+
| concat(‘11’,‘22’,‘33’) |
±-----------------------+
| 112233 |
±-----------------------+
1 row in set (0.00 sec)

MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
mysql> select concat(‘11’,‘22’,null);
±-----------------------+
| concat(‘11’,‘22’,null) |
±-----------------------+
| NULL |
±-----------------------+
1 row in set (0.00 sec)

你可能感兴趣的:(mysql行转列)