Difference between Concat and concat_ws () and the string functions of group_concat () and repeat ()

Source: Internet
Author: User

1. Concat () function
1.1 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)

The Concat function of Oracle can only connect two strings

SQL> select Concat ('11', '22') from dual;

1.2 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)

When Oracle Concat functions are connected, null is not returned as long as a string is not null.

SQL> select Concat ('11', null) from dual;
Concat
--
11

2. The concat_ws () function indicates Concat with separator, which has a separator string connection.
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 Concat, 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 () can be used for Row-to-column conversion. Oracle does not have such a function (mysql4.1 or above)

The complete syntax is as follows:
Group_concat ([distinct] field to be connected [order by ASC/DESC sorting field] [separator 'delimiter'])
Example
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)
3.1 group by ID and print the value of the Name field in one row, separated by commas (, by 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)

3.2 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)

3.3 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)

3.4 group by ID, print the value of the Name field in one row, separate them by commas, and 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. The repeat () function is used to copy a string. '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)

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/shuicaohui5/archive/2008/10/23/3129489.aspx

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.