Explain the use of the 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:

1 SELECTCONCAT(CAST(int_col ASCHAR), char_col)

MySQL's concat function can connect one or more strings, such as

123456789101112131415 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.

1234567 mysql> select concat(‘11‘,‘22‘,null);+------------------------+| concat(‘11‘,‘22‘,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

12345678 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

1234567 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 connect [order by Asc/desc sort field] [Separator ' delimiter '])

Basic Query

123456789101112 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, comma separated (default)

123456789 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 one line, semicolon delimited

123456789 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, print the value of the redundant Name field to a line,

Comma delimited

123456789 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 line, comma separated, and reverse by name

123456789 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 ' represents the string to be copied, 2 indicates the number of copies copied

123456789 mysql> selectrepeat(‘ab‘,2); +----------------+| repeat(‘ab‘,2) |+----------------+| abab      |+----------------+ 1 row in set(0.00 sec)

And AS

12345678 mysql> selectrepeat(‘a‘,2); +---------------+| repeat(‘a‘,2) |+---------------+| aa      |+---------------+1 row in set(0.00 sec)

Explain the use of the 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.