May 15, 2021 MySQL
How to use it:
CONCAT(str1,str2,...)
Returns a string that results from connection parameters. If any of the parameters are NULL, the return value is NULL.
Attention:
If all parameters are non-binary strings, the result is non-binary strings.
If the argument contains any binary string, the result is a binary string.
A numeric argument is converted into an equivalent binary string format;
SELECT CONCAT(CAST(int_col AS CHAR), char_col)
MySQL's concat function can connect one or more strings, for example
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)
MySQL's concat function returns NULL when connecting strings, as long as one of them is NULL
mysql> select concat('11','22',null);
+------------------------+
| concat('11','22',null) |
+------------------------+
| NULL |
+------------------------+
1 row in set (0.00 sec)
There is no function concat_ws MySQL
How to use it:
CONCAT_WS(separator,str1,str2,...)
CONCAT_WS () stands for CONCAT With With Issuer and is a special form of CONCAT(). T
he first argument is a separator for other parameters. T
he separator is positioned between the two strings to be connected.
Separators can be a string or other parameters.
Attention:
If the separator is NULL, the result is NULL.
The function ignores the NULL value after any separator arguments.
If connected separated by a comma
mysql> select concat_ws(',','11','22','33');
+-------------------------------+
| concat_ws(',','11','22','33') |
+-------------------------------+
| 11,22,33 |
+-------------------------------+
1 row in set (0.00 sec)
和MySQL中concat函数不同的是, concat_ws函数在执行的时候,不会因为NULL值而返回NULL
mysql> select concat_ws(',','11','22',NULL);
+-------------------------------+
| concat_ws(',','11','22',NULL) |
+-------------------------------+
| 11,22 |
+-------------------------------+
1 row in set (0.00 sec)
The function is group_concat MySQL
The full syntax is as follows:
group_concat (The fields to be connected by the Order BY ASC/DESC sorting fields)
Basic queries
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)
Group by id, print the value of the name field on one line, and the comma separates (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)
Group by id, print the value of the name field on a row, separated by a sign
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)
Group by id and print the value of the redundant name field on a row.
Comma separation
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)
Group by id, print the value of the name field on a row, separate the commas, and reverse the name 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)
Repeat() function
Used to copy strings, the following 'ab' represents the string to be copied, and 2 represents 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)
Mysql appends a string to a field in the table:
update table_name set field=CONCAT(field,'',str)
Mysql adds a string to a field in the table
update table_name set field=CONCAT('str',field)