Mysql string connection Function

Source: Internet
Author: User
1. CONCAT (str1, str2 ,...) Returns the string generated by the connection parameter. If any parameter is NULL, the return value is NULL. Mysqlselectconcat (112233, 33); ------------------------ | concat (, 33) | -------------------------- | --------------

1. CONCAT (str1, str2 ,...) Returns the string generated by the connection parameter. If any parameter is NULL, the return value is NULL. Mysql select concat ('11', '22', '33'); ---------------------- | concat ('11', '22', '33') | ---------------------- | 112233 | --------------

1,CONCAT (str1, str2 ,...)Returns the string generated by the connection parameter. If any parameter is NULL, the return value is NULL.

Mysql> select concat ('11', '22', '33 ');
+ ------------------------ +
| Concat ('11', '22', '33') |
+ ------------------------ +
| 1, 112233 |
+ ------------------------ +
1 row in set (0.00 sec)

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

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)


3,Group_concat

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)


4. 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)


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)

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.