MySQL的行转列 列转行 连接字符串 concat concat_ws group_concat函数用法

  • 创建MySQL数据库表结构
DROP TABLE IF EXISTS `weijing`;
CREATE TABLE `weijing` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(20) DEFAULT NULL,
`Date` date DEFAULT NULL,
`Scount` int(11) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
  • 插入表记录
INSERT INTO `weijing` VALUES ('1', '潍鲸', '2020-05-01', '10000');
INSERT INTO `weijing` VALUES ('2', '文章', '2020-05-01', '20000');
INSERT INTO `weijing` VALUES ('3', '潍鲸', '2020-05-02', '30000');
INSERT INTO `weijing` VALUES ('4', '文章', '2020-05-02', '35000');
INSERT INTO `weijing` VALUES ('5', '潍鲸', '2020-05-03', '31000');
INSERT INTO `weijing` VALUES ('6', '文章', '2020-05-03', '36000');
INSERT INTO `weijing` VALUES ('7', '潍鲸', '2020-05-04', '35000');
INSERT INTO `weijing` VALUES ('8', '文章', '2020-05-04', '38000');
  • 查看插入的表数据
mysql> select * from weijing;
+----+--------+------------+--------+
| Id | Name   | Date       | Scount |
+----+--------+------------+--------+
|  1 | 潍鲸   | 2020-05-01 |  10000 |
|  2 | 文章   | 2020-05-01 |  20000 |
|  3 | 潍鲸   | 2020-05-02 |  30000 |
|  4 | 文章   | 2020-05-02 |  35000 |
|  5 | 潍鲸   | 2020-05-03 |  31000 |
|  6 | 文章   | 2020-05-03 |  36000 |
|  7 | 潍鲸   | 2020-05-04 |  35000 |
|  8 | 文章   | 2020-05-04 |  38000 |
+----+--------+------------+--------+
8 rows in set (0.00 sec)
  • 列转行统计数据
mysql> SELECT Date , MAX(CASE NAME WHEN '文章' THEN Scount ELSE 0 END ) 文章,  MAX(CASE NAME WHEN '潍鲸' THEN Scount ELSE 0 END ) 潍鲸   FROM weijing  GROUP BY Date;
+------------+--------+--------+
| Date       | 文章   | 潍鲸   |
+------------+--------+--------+
| 2020-05-01 |  20000 |  10000 |
| 2020-05-02 |  35000 |  30000 |
| 2020-05-03 |  36000 |  31000 |
| 2020-05-04 |  38000 |  35000 |
+------------+--------+--------+
4 rows in set (0.00 sec)
  • 行转列统计数据
mysql> select date,group_concat(name,'统计:',scount) as '列转行' from weijing group by date;
+------------+---------------------------------------+
| date       | 列转行                                |
+------------+---------------------------------------+
| 2020-05-01 | 潍鲸统计:10000,文章统计:20000         |
| 2020-05-02 | 潍鲸统计:30000,文章统计:35000         |
| 2020-05-03 | 潍鲸统计:31000,文章统计:36000         |
| 2020-05-04 | 潍鲸统计:35000,文章统计:38000         |
+------------+---------------------------------------+
4 rows in set (0.00 sec)

mysql> select date,group_concat(name,'统计:',scount) as '列转行' from weijing group by date,name;
+------------+--------------------+
| date       | 列转行             |
+------------+--------------------+
| 2020-05-01 | 文章统计:20000     |
| 2020-05-01 | 潍鲸统计:10000     |
| 2020-05-02 | 文章统计:35000     |
| 2020-05-02 | 潍鲸统计:30000     |
| 2020-05-03 | 文章统计:36000     |
| 2020-05-03 | 潍鲸统计:31000     |
| 2020-05-04 | 文章统计:38000     |
| 2020-05-04 | 潍鲸统计:35000     |
+------------+--------------------+
8 rows in set (0.00 sec)
  • MySQL的行转列、列转行、连接字符串  concat、concat_ws、group_concat函数用法
使用方法:
CONCAT(str1,str2,…)
返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。
注意:
如果所有参数均为非二进制字符串,则结果为非二进制字符串。
如果自变量中含有任一二进制字符串,则结果为一个二进制字符串。
一个数字参数被转化为与之相等的二进制字符串格式;若要避免这种情况,可使用显式类型 cast, 例如:
SELECT CONCAT(CAST(int_col AS CHAR), char_col)
MySQL的concat函数可以连接一个或者多个字符串:
mysql> select concat(1);
+-----------+
| concat(1) |
+-----------+
| 1 |
+-----------+
1 row in set (0.00 sec)

mysql> select concat(1,2,3);
+---------------+
| concat(1,2,3) |
+---------------+
| 123 |
+---------------+
1 row in set (0.00 sec)
MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
mysql> select concat(1,2,3,null);
+--------------------+
| concat(1,2,3,null) |
+--------------------+
| NULL |
+--------------------+
1 row in set (0.00 sec)
MySQL中concat_ws函数
使用方法:
CONCAT_WS(separator,str1,str2,…)
CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式。第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。
注意:
如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。
concat_ws函数在执行的时候,不会因为NULL值而返回NULL
mysql> select concat_ws(',',1,2,3,null);
+---------------------------+
| concat_ws(',',1,2,3,null) |
+---------------------------+
| 1,2,3 |
+---------------------------+
1 row in set (0.00 sec)

mysql> select concat_ws(null,1,2,3);
+-----------------------+
| concat_ws(null,1,2,3) |
+-----------------------+
| NULL |
+-----------------------+
1 row in set (0.00 sec)
MySQL中group_concat函数
完整的语法如下:
group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator ‘分隔符’])
以name分组,把date字段的值打印在一行,逗号分隔(默认)
mysql> select name,group_concat(date) from weijing group by name;
+--------+-----------------------------------------------------------------------------------------+
| name   | group_concat(date)                                                                      |
+--------+-----------------------------------------------------------------------------------------+
| 文章   | 2020-05-04,2020-05-02,2020-05-04,2020-05-03,2020-05-03,2020-05-02,2020-05-01,2020-05-01 |
| 潍鲸   | 2020-05-03,2020-05-04,2020-05-02,2020-05-01,2020-05-04,2020-05-03,2020-05-02,2020-05-01 |
+--------+-----------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
以name分组,把date字段的值打印在一行,分号分隔。
mysql> select name,group_concat(date separator ';') from weijing group by name;
+--------+-----------------------------------------------------------------------------------------+
| name   | group_concat(date separator ';')                                                        |
+--------+-----------------------------------------------------------------------------------------+
| 文章   | 2020-05-04;2020-05-02;2020-05-04;2020-05-03;2020-05-03;2020-05-02;2020-05-01;2020-05-01 |
| 潍鲸   | 2020-05-03;2020-05-04;2020-05-02;2020-05-01;2020-05-04;2020-05-03;2020-05-02;2020-05-01 |
+--------+-----------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
以name分组,把去冗余的date字段的值打印在一行,逗号分隔。
mysql> select name,group_concat(distinct date) from weijing group by name;
+--------+---------------------------------------------+
| name   | group_concat(distinct date)                 |
+--------+---------------------------------------------+
| 文章    | 2020-05-04,2020-05-02,2020-05-03,2020-05-01 |
| 潍鲸    | 2020-05-03,2020-05-04,2020-05-02,2020-05-01 |
+--------+---------------------------------------------+
2 rows in set (0.01 sec)
以name分组,把date字段的值打印在一行,逗号分隔,以date排倒序.
mysql> select name,group_concat(date order by date desc) from weijing group by name;
+--------+-----------------------------------------------------------------------------------------+
| name | group_concat(date order by date desc) |
+--------+-----------------------------------------------------------------------------------------+
| 文章 | 2020-05-04,2020-05-04,2020-05-03,2020-05-03,2020-05-02,2020-05-02,2020-05-01,2020-05-01 |
| 潍鲸 | 2020-05-04,2020-05-04,2020-05-03,2020-05-03,2020-05-02,2020-05-02,2020-05-01,2020-05-01 |
+--------+-----------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
repeat()函数,用来复制字符串,如下’潍鲸’表示要复制的字符串,3表示复制的份数.
mysql> select repeat('潍鲸',3);
+--------------------+
| repeat('潍鲸',3)   |
+--------------------+
| 潍鲸潍鲸潍鲸       |
+--------------------+
1 row in set (0.00 sec)

发表评论