Talking about the concat function in mysql, mysql adds a string before/after the field, and mysqlconcat

Source: Internet
Author: User

Talking about the concat function in mysql, mysql adds a string before/after the field, and mysqlconcat

How to Use the concat function in MySQL:

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') |
+ ------------------------ +
| 1, 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)

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)

This function may be of great help to you !!

In this article, we will talk about the concat function in mysql. Adding a string before/after the mysql field is all the content that I have shared with you. I hope you can give us a reference and support the help house.

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.