Describes the usage of the concat function (connection string) and mysqlconcat in MySQL.
Concat functions in MySQL
Usage:
CONCAT (str1, str2 ,...)
Returns the string generated by the connection parameter. If any parameter is NULL, the return value is NULL.
Note:
If all parameters are non-binary strings, the result is a non-binary string.
If the independent variable contains any binary string, the result is a binary string.
A numeric parameter is converted to an equivalent binary string format. To avoid this, you can use an explicit cast type, for example:
SELECT CONCAT(CAST(int_col AS CHAR), char_col)
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') |+------------------------+| 112233 |+------------------------+1 row in set (0.00 sec)
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)
Concat_ws function in MySQL
Usage:
CONCAT_WS (separator, str1, str2 ,...)
CONCAT_WS () represents CONCAT With Separator, which is a special form of CONCAT. The first parameter is the delimiter of other parameters. The separator is placed between the two strings to be connected. The delimiter can be a string or another parameter.
Note:
If the Delimiter is NULL, the result is NULL. The function ignores the NULL value after any separator parameter.
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 the concat function in MySQL, 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)
Group_concat function in MySQL
The complete syntax is as follows:
Group_concat ([DISTINCT] field to be connected [Order by asc/DESC sorting field] [Separator 'delimiter'])
Basic Query
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 and print the value of the name field in one row, 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)
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)
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)
Group by id, print the value of the name field in one row, and separate the values with commas (,). 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)
Repeat () function
Used to copy a string. The following '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)
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.