MySQL in contact, Group_concat, CONCAT_WS, repeat

Source: Internet
Author: User
Tags mysql in

Yi, 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.

Mysql> Select Concat (' One ', ' one ', ' $ '); +------------------------+| Concat (' One ', ' a ', ' |+ ')------------------------+| 112233 |+------------------------+1 row in Set (0.00 sec) MySQL concat function when the connection string, as long as one of them is null, then return nullmysql> Select Concat (' One ', ' n ', null); +------------------------+| Concat (' One ', ' a ', ' null ') |+------------------------+| NULL   |+------------------------+1 row in Set (0.00 sec)

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

If the delimiter is null, the result is null. The function ignores NULL values after any delimiter parameters.

Mysql> Select Concat_ws (', ', ' one ', ' a ', ') ', +-------------------------------+| Concat_ws (', ', ' one ', ' a ', ' |+ ')-------------------------------+| 11,22,33 |+-------------------------------+1 row in Set (0.00 sec) differs from the concat function in MySQL, when the CONCAT_WS function is executed, Does not return NULL because of a null value mysql> Select CONCAT_WS (', ', ' one ', ' n ', null); +-------------------------------+| Concat_ws (', ', ' one ', ' |+ ', NULL) the-------------------------------+| 11,22 |+-------------------------------+1 row in Set (0.00 sec)

Third, Group_conca

Group_concat ([DISTINCT] field to connect [order by Asc/desc sort field] [Separator ' delimiter '])

Mysql>select * from AA;+------+------+| id| Name |+------+------+|1 | 10| | 1 | 20| | 1 | 20| | 2 | 20| | 3 | 200 | | 3 | |+------+------+6 rows in Set (0.00 sec) is grouped by ID, printing the value of the Name field on one line, separated by commas (default) mysql>Select Id,group_concat (name) from the AA group by ID;+------+--------------------+| id| Group_concat (name) |+------+--------------------+|1 | 10,20,20| | 2 | 20 | | 3 | 200,500|+------+--------------------+3 rows in Set (0.00 sec) is grouped by ID, printing the value of the Name field on one line, separated by semicolons mysql>Select ID,GROUP_CONCAT (name separator '; ') from the 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) is grouped by ID, printing the value of the de-redundant name field on one line, separated by commas mysql>Select Id,group_concat (distinct name) from the AA group by ID;+------+-----------------------------+| id| Group_concat (distinct name) |+------+-----------------------------+|1 | 10,20| | 2 | 20 | | 3 | 200,500 |+------+-----------------------------+3 rows in Set (0.00 sec) is grouped by ID, printing the value of the Name field in one line, separated by commas, and reversed 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) |+------+---------------------------------------+|1 | 20,20,10 | | 2 | 20| | 3 | 500,200|+------+---------------------------------------+3 rows in Set (0.00 sec)

Iv. 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           Select Repeat (' a ', 2); +---------------+| Repeat (' a ', 2) |+---------------+| AA            |+---------------+1 row in Set (0.00 sec)

MySQL in contact, Group_concat, CONCAT_WS, repeat

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.