concat function in MySQL (connection string)

Source: Internet
Author: User
Tags string format

concat function in MySQL
How to use:
CONCAT (STR1,STR2,...)

Returns the string that results from the connection parameter. If any one of the arguments is NULL, the return value is null.

Attention:
If all parameters are non-binary strings, the result is a non-binary string.
If the argument contains any twos binary string, the result is a binary string.
A numeric parameter is converted to an equal binary string format; To avoid this, you can use explicit type cast, for example:
SELECT CONCAT (CAST (Int_col as CHAR), Char_col)

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)

MySQL's concat function will return NULL if one is null when the string is concatenated.
Mysql> Select Concat (' One ', ' n ', null);
+------------------------+
| Concat (' One ', ' + ', null) |
+------------------------+
| NULL |
+------------------------+
1 row in Set (0.00 sec)

Concat_ws function in MySQL
How to use:
Concat_ws (SEPARATOR,STR1,STR2,...)

Concat_ws () represents CONCAT with Separator, which is a special form of the CONCAT (). The first parameter is the delimiter for the other parameter. The position of the delimiter is placed between the two strings to be concatenated. The delimiter can be a string, or it can be another parameter.
Attention:
If the delimiter is null, the result is null. The function ignores NULL values after any delimiter parameters.

Separated by commas, if connected
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 for null values when it executes
Mysql> Select Concat_ws (', ', ' one ', ' n ', NULL);
+-------------------------------+
| Concat_ws (', ', ' one ', ' + ', 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 connect [order by Asc/desc sort field] [Separator ' delimiter '])

Basic Query

Mysql> select * from AA;
+------+------+
| id| name |
+------+------+
| 10|
| 20|
| 20|
| 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, comma separated (default)

Mysql> Select Id,group_concat (name) from the AA group by ID;
+------+--------------------+
| id| Group_concat (name) |
+------+--------------------+
| 10,20,20|
| 20 |
|3 | 200,500|
+------+--------------------+
3 Rows in Set (0.00 sec)

Group by ID, print the value of the Name field on one line, semicolon delimited

Mysql> Select ID,GROUP_CONCAT (name separator '; ') from the AA group by ID;
+------+----------------------------------+
| id| Group_concat (name separator '; ') |
+------+----------------------------------+
| 10;20;20 |
| 20|
|3 | 200;500 |
+------+----------------------------------+
3 Rows in Set (0.00 sec)

Group by ID, print the value of the redundant Name field to a line,

Comma delimited

Mysql> Select Id,group_concat (distinct name) from the AA group by ID;
+------+-----------------------------+
| id| Group_concat (distinct name) |
+------+-----------------------------+
| 10,20|
| 20 |
|3 | 200,500 |
+------+-----------------------------+
3 Rows in Set (0.00 sec)

Group by ID, print the value of the Name field in one line, comma separated, and reverse by name

Mysql> Select Id,group_concat (name order BY name Desc) from the AA group by ID;
+------+---------------------------------------+
| id| Group_concat (name order BY name Desc) |
+------+---------------------------------------+
| 20,20,10 |
| 20|
|3 | 500,200|
+------+---------------------------------------+
3 Rows in Set (0.00 sec)

Repeat () function

Used to copy a string, the following ' AB ' represents the string to be copied, 2 indicates the number of copies copied

Mysql> Select repeat (' AB ', 2);

+----------------+
| Repeat (' AB ', 2) |
+----------------+
| Abab |
+----------------+

1 row in Set (0.00 sec)

And AS
Mysql> Select repeat (' a ', 2);

+---------------+
| Repeat (' a ', 2) |
+---------------+
| AA |
+---------------+
1 row in Set (0.00 sec)

concat function in MySQL (connection string)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.