1. concat () function
1.1 The concat function of MySQL can connect one or more strings, such
Mysql> select concat ('10 ');
+ -------------- +
| Concat ('10') |
+ -------------- +
| 10 |
+ -------------- +
1 row in set (0.00 sec)
Mysql> select concat ('11', '22', '33 ');
+ ------------------------ +
| Concat ('11', '22', '33') |
+ ------------------------ +
| 1, 112233 |
+ ------------------------ +
1 row in set (0.00 sec)
The concat function of Oracle can only connect two strings
SQL> select concat ('11', '22') from dual;
1.2 When the concat function of MySQL connects to a string, if one of them is NULL, NULL is returned.
Mysql> select concat ('11', '22', null );
+ ------------------------ +
| Concat ('11', '22', null) |
+ ------------------------ +
| NULL |
+ ------------------------ +
1 row in set (0.00 sec)
When Oracle concat functions are connected, NULL is not returned as long as a string is not NULL.
SQL> select concat ('11', NULL) from dual;
CONCAT
--
11
2. The concat_ws () function indicates concat with separator, which has a separator string connection.
For example, separate the strings with commas (,).
Mysql> select concat_ws (',', '11', '22', '33 ');
+ ------------------------------- +
| Concat_ws (',', '11', '22', '33') |
+ ------------------------------- +
| 11,22, 33 |
+ ------------------------------- +
1 row in set (0.00 sec)
Unlike concat, The concat_ws function does not return NULL because of the NULL value during execution.
Mysql> select concat_ws (',', '11', '22', NULL );
+ ------------------------------- +
| Concat_ws (',', '11', '22', NULL) |
+ ------------------------------- +
| 11,22 |
+ ------------------------------- +
1 row in set (0.00 sec)
3. group_concat () can be used for Row-to-column conversion. Oracle does not have such a function.
The complete syntax is as follows:
Group_concat ([DISTINCT] field to be connected [Order by asc/DESC sorting field] [Separator 'delimiter'])
Example
Mysql> select * from aa;
+ ------ +
| Id | name |
+ ------ +
| 1 | 10 |
| 1 | 20 |
| 1 | 20 |
| 2 | 20 |
| 3 | 200 |
| 3 | 500 |
+ ------ +
6 rows in set (0.00 sec)
3.1 group by id and print the value of the name field in one row, separated by commas (, by default)
Mysql> select id, group_concat (name) from aa group by id;
+ ------ + -------------------- +
| Id | group_concat (name) |
+ ------ + -------------------- +
| 1 | 10, 20, 20 |
| 2 | 20 |
| 3 | 200,500 |
+ ------ + -------------------- +
3 rows in set (0.00 sec)
3.2 group by id, print the value of name field in one row, separated by semicolons
Mysql> select id, group_concat (name separator ';') from aa group by id;
+ ------ + ------------------------------------ +
| Id | group_concat (name separator ';') |
+ ------ + ------------------------------------ +
| 1 | 10; 20; 20 |
| 2 | 20 |
| 3 | 200, 500 |
+ ------ + ------------------------------------ +
3 rows in set (0.00 sec)
3.3 group by id and print the value of the redundant name field in one row, separated by commas
Mysql> select id, group_concat (distinct name) from aa group by id;
+ ------ + ----------------------------- +
| Id | group_concat (distinct name) |
+ ------ + ----------------------------- +
| 1 | 10, 20 |
| 2 | 20 |
| 3 | 200,500 |
+ ------ + ----------------------------- +
3 rows in set (0.00 sec)
3.4 group by id, print the value of the name field in one row, separate them by commas, and sort the values by name in reverse order.
Mysql> select id, group_concat (name order by name desc) from aa group by id;
+ ------ + --------------------------------------- +
| Id | group_concat (name order by name desc) |
+ ------ + --------------------------------------- +
| 1 | 20, 20, 10 |
| 2 | 20 |
| 3 | 500,200 |
+ ------ + --------------------------------------- +
3 rows in set (0.00 sec)
4. The repeat () function is used to copy a string. 'AB' indicates the string to be copied, and 2 indicates the number of copies.
Mysql> select repeat ('AB', 2 );
+ ---------------- +
| Repeat ('AB', 2) |
+ ---------------- +
| Abab |
+ ---------------- +
1 row in set (0.00 sec)
Ru
Mysql> select repeat ('A', 2 );
+ --------------- +
| Repeat ('A', 2) |
+ --------------- +
| Aa |
+ --------------- +
1 row in set (0.00 sec)