The difference between concat and concat_ws() and several practical string functions of MySQL

[size=medium]1, concat() function
1.1 MySQL's concat function can connect one or more strings, such as
mysql> select concat('10');
+--------------+
| concat('10') |
+--------------+
| 10           |
+--------------+
1 row in set (0.00 sec)
 
mysql> select concat('11','22','33');
+------------------------+
| concat('11','22','33') |
+------------------------+
| 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 strings, as long as one of them is NULL, it will return NULL

mysql> select concat('11','22',null);
+------------------------+
| concat('11','22',null) |
+------------------------+
| NULL                   |
+------------------------+
1 row in set (0.00 sec)


When Oracle's concat function is connected, as long as there is a string that is not NULL, it will not return NULL

SQL> select concat('11',NULL) from dual;
CONCAT
--
11


2. The concat_ws() function, which means concat with separator, that is, a string connection with a separator,
such as a comma-separated connection after the connection
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 will not return NULL because of the NULL value when it is executed.
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 to convert rows to columns. Oracle does not have such a function. The

complete syntax is as follows
group_concat([DISTINCT] The field to be connected [Order BY ASC/DESC sorting field] [Separator 'Separator']) The
following 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, print the value of the name field on one line, separated by commas (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 the name field on one line, 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, print the value of the de-redundant name field on one line, 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 on one line, separated by commas, in reverse order by name

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 strings. The following 'ab' indicates the string to be copied, and 2 indicates the number of copies to be copied.

mysql> select repeat('ab',2);
 
+----------------+
| repeat('ab',2) |
+----------------+
| abab |
+----------------+
 
1 row in set (0.00 sec)


another example
mysql> select repeat('a',2);
 
+---------------+
| repeat('a',2) |
+---------------+
| aa            |
+---------------+
1 row in set (0.00 sec)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326485295&siteId=291194637